Requiring modules is simple
var m = require('./my_module'); m.myFunc(foo, bar);The file needs to be "./my_module.js", with the following exports line:
var myFunc = function () { ... }; module.exports.myFunc = myFunc;The Why is the hard part. You can't do
exports = {}
, but you can do module.exports = {}
. Turns out module
is the "global" namespace in an require()
file. exports
is some sort of "alias" for module.exports
(I haven't figured that out). What this means is that exports.myFunc = myFunc;
is OK, but exports = {myFunc: myFunc}
is NOT OK, BUT module.exports = {myFunc: myFunc}
is OK. Basically, I've decided not to use this pseudo-whatchamacalit "alias" type thing exports
. I'll just use module.exports
. Additionally, it is probably best to use the form:module.exports.myFunc = function () { ... };for all exported functions inside
./my_module.js
No comments:
Post a Comment