CommonJS is a module specification that was originally applied to Nodejs as the module specification for Nodejs.
JavaScript running on the browser side also lacks a similar specification, so before ES6 came out, the same module specification was implemented on the front end (e.g. AMD) to manage the front end modules.
Since ES6, a new ES6 Module specification has been introduced, which implements Module functions at the level of language standards and is quite simple to implement. It is expected to become a common Module solution for browsers and servers.
However, ES6 Module is not compatible with browsers at present. Export and import that we usually use in Webpack will be converted to CommonJS by Babel.
The main differences in use are:
The CommonJS module prints a copy of the value, the ES6 module prints a reference to the value.
The CommonJS module is run time loaded, and the ES6 module is compile time output interface.
CommonJs is a single value export, ES6 Module can export multiple values
CommonJs dynamic syntax can be written in judgment, ES6 Module static syntax can only be written at the top level
Link: www.zhihu.com/question/62…