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

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -