es-module-example-mjs
ES module (.mjs) that can be used natively from node 8.5.0+, without transpilers.
Starting with version 8.5.0, Node.js supports ES modules natively, behind a command line option. Read more at 2ality - Using ES modules natively in Node.js.
How this works
To publish a native ES module, simply define main
in package.json
to point to a file with the .mjs
extension:
That's the only change. Your existing transpilation process to support older Node versions will work as before - just make sure to point Babel to the .mjs
file(s).
Usage
- Install the module:
yarn add mjs-example# or, npm install mjs-example
- Create a test file:
;console;
- Run
node
(v8.5.0+) with the--experimental-modules
flag:
node --experimental-modules mjs-test.mjs