Node.js "require" functions and parameters

When I do this:

lib = require('lib.js')(app) 

is there an app actually getting transferred to?

in lib.js:

 exports = module.exports = function(app){} 

It doesn't seem to, because when I try to do more than just (app) and instead:

 lib = require('lib.js')(app, param2) 

and

 exports = module.exports = function(app, param2){} 

I do not get params2 .

I tried to debug:

 params = {} params.app = app params.param2 = "test" lib = require("lib.js")(params) 

but in lib.js, when I try JSON.stringify to get this error:

 "DEBUG: TypeError: Converting circular structure to JSON" 
+55
javascript require
Sep 09 '11 at 9:50 a.m.
source share
2 answers

When you call lib = require("lib.js")(params)

In fact, you are calling lib.js with one parameter containing two property names app and param2

You either want

 // somefile require("lib.js")(params); // lib.js module.exports = function(options) { var app = options.app; var param2 = options.param2; }; 

or

 // somefile require("lib.js")(app, param2) // lib.js module.exports = function(app, param2) { } 
+78
Sep 09 '11 at 9:59 a.m.
source share

You may have an undefined value that you are trying to pass.

Take for example requires.js :

 module.exports = exports = function() { console.log('arguments: %j\n', arguments); }; 

When you call it correctly, it works:

 node > var requires = require('./requires')(0,1,2,3,4,5); arguments: {"0":0,"1":1,"2":2,"3":3,"4":4,"5":5} 

If you have a syntax error, it fails:

 > var requires = require('./requires')(0,); ... var requires = require('./requires')(0,2); ... 

If you have an undefined object, it does not work:

 > var requires = require('./requires')(0, undefined); arguments: {"0":0} 

So, I first checked that your object was correctly defined (and spelled correctly when you pass it), and then make sure you have no syntax errors.

+17
Sep 09 '11 at 22:00
source share



All Articles