0

How do you access a function that is inside one of the scripts that you have "included" using Node's require() function?

--main-js.js--

var webnotis = require('./modules/web-notification.js')

--web-notification.js--

function getURL(host, path) {
...
}

Also how would I use this function in other required scripts?


--report-tables.js--

var cltvOut;
exports.cltv = function cltv(getURL)
{
  clearTimeout(cltvOut);
  cltvOut = setTimeout(function(){
    if(exports.getURL('192.168.0.15', '/IMS4/Reports/calculateCLTV'))
    {
      cltv();
    } else {
      console.log('CLTV error.')
    }
  }, 2000);
}

webnotis2 = require('./web-notification.js')
var cltvOut;
exports.cltv = function cltv()
{
  clearTimeout(cltvOut);
  cltvOut = setTimeout(function(){
    if(webnotis2.getUrl('192.168.0.15', '/IMS4/Reports/calculateCLTV'))
    {
      cltv();
    } else {
      console.log('CLTV error.')
    }
  }, 2000);
}

3 Answers 3

2

If it is not a part of module.exports then you can't. For example:

web-notification.js

function getURL(host, path) {
...
}

module.exports = exports = {
    getURL: getURL
};

main-js.js

var webnotis = require('./modules/web-notification.js');
webnotis.getURL(...);
Sign up to request clarification or add additional context in comments.

8 Comments

I have tried that but when I try to access it within another module it tells me the object does not exist. Please see edit.
@imperium2335: If the function is defined and exported by another module, you don't access it via exports.getURL. exports only holds the values that should be exported by the current module. You have to access it via the variable you assigned the module to, just like freakish showed in his answer. Maybe you should have a look again at how modules work: nodejs.org/api/modules.html.
@FelixKling I have read it and the example but it does not work when I call it from within another module.
@imperium2335: In web-notification.js, you have to do exports.getURL = getURL; and in main-js.js you have to do webnotis.getURL(...). That's it. If that does not work, then either your module cannot be loaded because you provided the wrong path to the file or you have an error in the module, or you really did something differently. There is not much we can do besides showing how it is done.
@FelixKling But what do I have to do in report-tables.js? Which is also a module thats required in main-js.js.
|
0

It's called exporting a module.

Sample from here :

create a file ./utils.js, and define the merge() function as seen below..

  function merge(obj, other) {

      //...
  };

  exports.merge = merge;

Now the merge function is accessible another JS in utils as:

var utils = require('./utils');

utils.merge();

Comments

0
   var webnotis = require('./modules/web-notification.js')
      var host='urhost';
      var path='urpath'; 
      webnotis.getURL(host,path,function(err,res){
         if(!err){
               console.log('url is '+res);
            }

      });

web-notification.js

     exports.getURL=function(host, path,cb) {
            var url=host+path;
             cb(null,url);
     }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.