modular specification
- commonjs specification: the modular specification in node is the commonjs specification (implemented by node itself), and the CommonJS module outputs a copy of a value
- esmodule specification (import export): ES6 modules export references to values
- umd specification (unified module specification, if the browser does not support commonjs requirejs, directly put the variable on the window)
- amd specification (requirejs): load modules asynchronously, and advocate pre-dependency and early execution
- cmd specification (seajs, outdated): Advocates relying on proximity and delaying execution.
commonjs specification
- Complex code can be split into small modules to facilitate code management and maintenance
- The content between each module is independent of each other and does not affect each other (to solve the problem of variable conflicts). Solution: the use of singleton mode cannot be completely solved, and self-executing functions can be used to solve it
Specification definition:
- Each file is a module
- If you want a variable in the module to be used by others, you can use to
module.exports
export this variable - If another module wants to use the results exported by this module, it needs to use
require
syntax to reference (synchronize)
Classification of modules
- Core modules, built-in modules : not written by yourself, nor installed, but provided by node itself, which can be used directly, such as:
require("fs")
- Third-party modules : modules written by others, installed through
npm install
installation , do not need to have a path, for example:require("commander")
- Custom module : The module defined by oneself is the module written by oneself, and the path (relative path, absolute path) needs to be added when referencing, for example:
require("./6/kaimo-promise.js")