https://github.com/dandv/es-module-example-mjs
ES module (.mjs) that can be used natively from node 8.5.0+, without transpilers
https://github.com/dandv/es-module-example-mjs
es-module es6 es6-modules example javascript sample
Last synced: 11 months ago
JSON representation
ES module (.mjs) that can be used natively from node 8.5.0+, without transpilers
- Host: GitHub
- URL: https://github.com/dandv/es-module-example-mjs
- Owner: dandv
- License: mit
- Created: 2017-09-25T05:39:14.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-09-25T06:02:35.000Z (over 8 years ago)
- Last Synced: 2025-03-02T03:52:26.634Z (11 months ago)
- Topics: es-module, es6, es6-modules, example, javascript, sample
- Homepage: https://medium.com/@dandv/publishing-native-es-modules-with-node-v8-5-0-730736e0f612
- Size: 3.91 KB
- Stars: 1
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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](http://2ality.com/2017/09/native-esm-node.html).
## How this works
To publish a native ES module, simply define `main` [in `package.json`](https://github.com/dandv/es-module-example-mjs/blob/master/package.json#L5) to point to a file with the `.mjs` extension:
```json
{
"name": "mjs-example",
"version": "1.0.2",
"description": "ES native module (.mjs) - requires node 8.5.0+",
"main": "example.mjs"
}
```
That's the only change. Your [existing transpilation process](https://github.com/dandv/local-iso-dt/blob/master/package.json#L13) to support older Node versions will work as before - just make sure to point Babel to the `.mjs` file(s).
## Usage
1. Install the module:
```bash
yarn add mjs-example
# or, npm install mjs-example
```
2. Create a [test file](https://github.com/dandv/es-module-example-mjs/blob/master/mjs-test.mjs):
```js
import {hello} from 'mjs-example';
console.log(hello);
```
3. Run `node` (v8.5.0+) with the `--experimental-modules` flag:
```bash
node --experimental-modules mjs-test.mjs
```