86

When I do:

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

is app actually geting passed in?

in lib.js:

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

Seems like no, since when I try to do more than just (app) and instead do:

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

And:

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

I don't get params2.

I've tried to debug by doing:

params = {}
params.app = app
params.param2 = "test"

lib = require("lib.js")(params)

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

"DEBUG: TypeError: Converting circular structure to JSON"
Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
user885355
  • 3,369
  • 4
  • 19
  • 10

2 Answers2

129

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

You're actually calling lib.js with one parameter containing two properties name 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) { }
UpTheCreek
  • 31,444
  • 34
  • 152
  • 221
Raynos
  • 166,823
  • 56
  • 351
  • 396
21

You may have an undefined value that you're trying to pass in.

Take for instance, 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 doesn't work:

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

So, I'd first check to see that your object is defined properly (and spelled properly when you pass it in), then check that you don't have syntax errors.

Joseph Yaduvanshi
  • 20,241
  • 5
  • 61
  • 69
  • 1
    Thank you. My mistake was that I forgot to change the number of arguments for one of the calls. – user885355 Sep 09 '11 at 22:22
  • Now I understand how this works a bit better. My remaining question is now about the JSON error. Do most people just debug through the browser? – user885355 Sep 09 '11 at 22:41
  • As far as I know, most people use `util.inspect` to inspect objects (this is done internally with `console.log`). You can also use node-inspector (see [article](http://howtonode.org/debugging-with-node-inspector) for video tutorial). node-inspector is the webkit inspector (like CTRL+SHIFT+J in Chrome) which listens to `node --debug`. – Joseph Yaduvanshi Sep 09 '11 at 23:37
  • Why did you also assign a variable called `exports` in line `module.exports = exports =...` ? – Sohail Si Feb 25 '19 at 19:28
  • 1
    @SohailSi This is a way to prevent any programmatic errors (like if someone binds a new object to both `exports` and `module.exports`). See https://stackoverflow.com/a/13622513/151445 for more details. – Joseph Yaduvanshi Feb 26 '19 at 20:14
  • 1
    Thank you @JimSchubert for the educational comment. – Sohail Si Feb 28 '19 at 10:44