Notes of ES Modules and CommonJS Modules
Node
Node version: v8.5.0
- Node Modules
- Node ECMAScript Modules
- The
--experimental-modules
flag can be used to enable features for loading ES modules - ES modules imports will be loaded asynchronously
- ES modules files will use
.mjs
extension The
.mjs
file extension will not be loadable viarequire()
ES consuming CommonJS
cjs.js
:
1 | module.exports = { v: 1 } |
esm.mjs
:
1 | import assert from 'assert' |
Unsupported Features
import()
import.meta
- Loader Hooks
Babel Transform Plugin
Babel version: v6.26.0 Babel plugin transform-es2015-modules-commonjs
Export
In:
1 | export default { v: 1 } |
Out:
1 | Object.defineProperty(exports, '__esModule', { value: true }) |
Import
In:
1 | import x from 'x' |
Out:
1 | const __x = require('x') |
In:
1 | import { y } from 'y' |
Out:
1 | const _y = require('y') |
In:
1 | import * as z from 'z' |
Out:
1 | const _z = require('z') |