javascript - Writing commonjs module and load it using require (not using relative path) -
what best pratice referencing local commonjs module without using relative path below?
var custmod= require(‘custommodule’); custmod(true); custmod.save(‘foo’);
is there reference building module this?
if wrote module below, getting undefined when call custmode.save(12);
module.exports = custommodule;function custommodule(mode) { var mode = 1; return { save: function (value) { console.log(mode, value) }, get: function (id) { console.log(mode, id) } }
you can add path require
check using
require.paths.push('/my/path');
or
require.main.paths.push('/my/path');
depending on node version.
then if custommodule.js
exists @ /my/path/custommodule.js
, can use
require('custommodule');
do note though, you'd need on every module intend use method on.
Comments
Post a Comment