CommonJs is a nodeJS modular tool that is widely used on the server.

a.js var x = 5; var addX = function (value) { return value + x; };

b.js var app = require('./index.js'); console.log(app.addX(3));

If b.js is introduced in HTML, you need to introduce require.js at the same time, otherwise the browser will report an error that does not recognize the require method.

2, ES6 modules, is the new MODULES object of ES6, built-in modularization syntax, there is no need to introduce additional RequireJS, written as follows:

a.js export default function add(value) { return value; }

b.js import app from 'index.js'; console.log(app(2));

The node b.js command cannot be executed on the terminal because Node does not recognize ES6 Modules. If b.js is introduced in an HTML file, the type in the

tag should be set to Modules, not text/javascript

<script type="module" src="b.js"></script>

If you use the browser to open the HTML file directly, the browser will report the following error: Cross-domain problem.

Access to script at 'file:///D:/commonTest/main.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.

The reason is that browsers import JS files through labels and use file protocol when loading JS files. This protocol will cause cross-domain problems, while HTTP, HTTPS and other protocols will not appear this problem. Therefore, you need to start the local service when using import and export.