What is best pratice for referencing a local commonjs module without using a relative path as below?
var custMod= require(‘customModule’);
custMod(true);
custMod.save(‘foo’);
Is there any reference for building a module like this?
If I wrote module like below, getting undefined when I 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)
}
}
require. Injecting global variables should be avoided. How the module can be loaded (i.e. via a direct path or just the name) cannot be influenced by the module itself. It depends on how the Node environment is set up. This may help to understand the issue: nodejs.org/api/modules.html