Js“ need”函数和参数

当我这样做:

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

app真的被传进去了吗?

在 lib.js 中:

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

似乎没有,因为当我尝试做的不仅仅是 (app),而是:

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

还有:

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

我不懂 params2

我曾试图通过以下方法进行调试:

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


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

但是在 lib.js 中,当我尝试 JSON.stringify时,我得到了这个错误:

"DEBUG: TypeError: Converting circular structure to JSON"
99094 次浏览

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) { }

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.