node.js - Do I need to 'require' a file if it is already required by another file -
i have 3 files
b requires a
c requires (b , a)
in scenario c needs require a? doubt because b requires , when c requires b should requires a.
so want sure whether c needs explicitly require or not.
upon same situation, when required explicitly :
i found following error :
module.js:340 throw err; ^ error: cannot find module 'a' @ function.module._resolvefilename (module.js:338:15) @ function.module._load (module.js:280:25) @ module.require (module.js:364:17) @ require (module.js:380:17) @ object.<anonymous> (/users/topi/controller.js:3:13) @ module._compile (module.js:456:26) @ object.module._extensions..js (module.js:474:10) @ module.load (module.js:356:32) @ function.module._load (module.js:312:12) @ module.require (module.js:364:17)
why so?
in cases, yes. each module needs reference module needs require()
independently.
why so?
when use:
var = require('./a');
this declares a
local variable current module, evaluated within own closure. why modules have exports
allow them specify "public" api.
it can suggested each module must list dependencies, such c
depending on a
, b
.
// c.js var = require('./a'); var b = require('./b');
but, use exports
pass-along 1 module through -- e.g. a
through b
:
// b.js var = exports.a = require('./a');
// c.js var b = require('./b'); console.log(b.a);
and, technically, attach a
global
. though, shouldn't. doing typically considered code smell.
Comments
Post a Comment