COMMONJS is the specification of server-side modules, which are used by node for promotion. Because of the complexity of server-side programming, it is difficult to interact with the operating system and other applications without modules. Here's how to use it:
According to the COMMONJS specification:
A separate file is a module. Each module is a separate scope, meaning that variables defined inside the module cannot be read by other modules unless they are defined as global
properties of the object.
The best way to output module variables is to use module.exports
objects.
The load module uses require
a method that reads a file and executes it, returning an object inside the file module.exports
Look closely at the code above and you'll notice that require
it's synchronized. The module system needs to read the module file contents synchronously and compile execution to get the module interface.
However, there are a lot of problems with the browser side.
On the browser side, the best and easiest way to load JavaScript is to document
insert <script>
tags in. But script tags are inherently asynchronous, and traditional CommonJS modules do not load properly in a browser environment.
One of the solutions is to develop a server-side component that makes static analysis of the module code and returns the module to the browser side, along with its dependency list. This works well, but requires the server to install additional components and therefore adjusts a series of underlying architectures.
Another approach is to encapsulate the module definition with a standard set of templates:
On the browser side, the best and easiest way to load JavaScript is to document
insert <script>
tags in. But script tags are inherently asynchronous, and traditional CommonJS modules do not load properly in a browser environment.
One of the solutions is to develop a server-side component that makes static analysis of the module code and returns the module to the browser side, along with its dependency list. This works well, but requires the server to install additional components and therefore adjusts a series of underlying architectures.
Another approach is to encapsulate the module definition with a standard set of templates:
The template code provides the opportunity for the module loader to perform static analysis of the module code and dynamically generate dependency lists before the module code executes.
turn from Https://www.jianshu.com/p/d67bc79976e6 front-end modularity (COMMONJS,AMD and cmd)
Modular Programming (ii)---common.js