Node.js - Module

Updated: 2018-05-27

Load a module:

Search from node_modules:

require("module_name");

Search from file:

require("./module_name");
require("/path/to/model_name");

Write a module, use exports

// in module
exports.foo = 10;

// outside module
var myModule = require("./module_name");
console.log(myModule.foo); // 10

or export as one object

// in module
var myModule = {};
myModule.foo = 10;
module.exports = myModule;

// outside module
var myModule = require("./module_name");
console.log(myModule.foo); // 10