NodeJS modules
NodeJS modules
A JavaScript file can be imported as a module into another file using the require
command. For example, he file myPackage.js can be imported as so:
var myModule= require('./myModule')
Here, myModule
becomes an object whose properties are that nested in the exports
object present in the myModule.js file:
exports.foo= "bar"
This allows the variable foo to be used in the code that imported myModule.js:
var myModule= require('./myModule')
console.log(myModule.foo) // Will print "bar"
However, exports
is an alias of module.exports. Consequently, whatever is assigned to exports is also available on module.exports. However, if something is assigned directly to exports, then the shortcut to module.exports is lost (from here