The module loading mechanism is known as the COMMONJS specification. Under this specification, each .js
file is a module that does not conflict with the variable name and function names used internally, for example, hello.js
and main.js
both declare global variables var s = ‘xxx‘
, but do not affect each other.
A module wants to expose a variable (function is also a variable) module.exports = variable;
, you can use, a module to refer to other modules var ref = require(‘module_name‘);
exposed variables, with the reference module to get the variable.
Summarize:
To output variables externally in the module, use:
module.exports = variable;
The output variable can be any object, function, array, and so on.
To introduce objects that are output by other modules, use:
var foo = require(‘other_module‘);
What is introduced specifically depends on the object that introduces the output of the module.
Node's COMMONJS specification