The method of operation of JavaScript ES6 or JS module

Run ES6 code (or modules) method:

1, may be used Babel transcoder, the ES6 is transcoded to the Node.js JS CommonJS.

  For example, when installed Babel, use the command babel file.js --out dir folder. I.e., the file name is "file" of JS modules (code) called transcoding "folder", in a folder. The need to use the JS code module code or transcode general JS, and then run the file after transcoding, can run. (Note: Babel also provides online transcoding: https://babeljs.io/repl)

2, talk about the extension ES6 file into .mjs from .js, and then add --experimental-modules in the node command to execute the code.

  For example, we want to run "moduleTry.js" file, first change its suffix .mjs, namely "moduleTry.mjs". Then execute Node command in the file directory, node --experimental-modules moduleTry.mjs it.

3, run ES6 module in the browser.

  Such as,

Guess you like

Origin www.cnblogs.com/xinkuiwu/p/11582296.html