Reprinted: https://www.jianshu.com/p/7c7fc199b7aa
- The CommonJS standard loading module is synchronous, and only after the loading is completed can the following operations be performed.
- The AMD specification is for asynchronously loading modules, allowing callback functions to be specified.
Since Node.js is mainly used for server programming, module files are generally already on the local hard disk, so it loads faster, and does not need to consider the asynchronous loading method, so the CommonJS specification is more applicable. However, if it is a browser environment, to load the module from the server side, you must use the asynchronous mode, so the browser side generally adopts the AMD specification.
CommonJS
In the specification module
, exports
andrequire
- Each file is a module and has its own scope. Inside each module, the
module
variable represents the current module and is an object, and itsexports
attributes (iemodule.exports
) are external interfaces. module.exports
The attribute indicates the current output interface of the module, and other files loading the module actually read themodule.exports
variables.- For convenience,
Node
provide aexports
variable for each module to point tomodule.exports
.
let exports = module.exports;
require
The command is used to load the module file.
Examples of use:
// name.js exports.name = function () { return '李婷婷'}; // export // getName.js let getName = require ('name'); // import
Note: You cannot directly exports
point the variable to a value, because this is equivalent to cutting off the exports
connection with module.exports
:
exports = function(x){console.log(x)}
If the external interface of a module is a single value, the exports
output cannot be used , only the module.exports
output.
AMD / CMD specifications
AMD
It isRequireJS
the standardized output of module definition in the promotion process.
CMD
It isSeaJS
the standardized output of module definition in the promotion process.- For the dependent modules, it
AMD
is executed in advance andCMD
delayed. ButRequireJS
from the2.0
beginning, it can be changed to perform delay (depending on the wording handled differently).CMD
Respectas lazy as possible
. -
CMD
Respect dependence on the nearest, andAMD
respect dependence on the front.
// CMD define ( function (require, exports, module) { let a = require ('./ a' ); a.doSomething (); · let b = require ('./ b'); // depend You can write b.doSomething (); ... }) // AMD recommends define (['./ a', './b'], function (a, b) { // dependence must start Just write a.doSomething () ... b.doSomething () ... })
Although it AMD
also supports CMD
the wording and also supports passing require
as a dependency, RequireJS
the author defaults to the above wording, which is also the default module definition in the official document.
-
AMD
TheAPI
default is a strict distinction between multiple uses, andCMD
aAPI
single responsibility.
For exampleAMD
, therequire
globalrequire
and local arerequire
called require.CMD
Here, there is no globalrequire
, but according to the completeness of the module system, it is providedseajs.use
to realize the loading start of the module system.CMD
Here, eachAPI
is simple and pure.
Expand
all current engines have not yet achieved import
, in node
the use of babel
support ES6
, it is only to ES6
transcode ES5
and then executed, import
syntax will be transcoded to require
. This is why it is used when the module is exported, module.exports
and it import
still works when the module is imported , because in essence, it import
will be transcoded require
to execute.
What is the difference between AMD and CMD in the reference document
CommonJS specification
?
Node does not understand require and import, you will be miserable