UNPKG

7.69 kBMarkdownView Raw
1# esm
2
3The brilliantly simple, babel-less, bundle-less ECMAScript module loader.
4
5> `esm` is the world’s most advanced ECMAScript module loader.
6This fast, production ready, zero dependency loader is all you need to support
7ECMAScript modules in Node 6+. See the release [post](https://medium.com/web-on-the-edge/tomorrows-es-modules-today-c53d29ac448c)
8and [video](https://www.youtube.com/watch?v=JcZ-FzfDq8A#t=5) for details!
9
10Install
11---
12
13* __New projects__
14
15 Run `npm init esm` or `yarn create esm`.
16
17 :bulb: Use the `-y` flag to answer “yes” to all prompts.
18
19* __Existing projects__
20
21 Run `npm i esm` or `yarn add esm`.
22
23Getting started
24---
25
26There are two ways to enable `esm`.
27
281. Enable `esm` for packages:
29
30 Use `esm` to load the main ES module and export it as CommonJS.
31
32 __index.js__
33 ```js
34 // Set options as a parameter, environment variable, or rc file.
35 require = require("esm")(module/*, options*/)
36 module.exports = require("./main.js")
37 ```
38 __main.js__
39 ```js
40 // ESM syntax is supported.
41 export {}
42 ```
43 :bulb: These files are automagically created with `npm init esm` or `yarn create esm`.
44
452. Enable `esm` for local runs:
46
47 ```shell
48 node -r esm main.js
49 ```
50 :bulb: Omit the filename to enable `esm` in the REPL.
51
52Features
53---
54
55:clap: By default, :100: percent CJS interoperability is enabled so you can get stuff done.<br>
56:lock: `.mjs` files are limited to basic functionality without support for `esm` options.
57
58Out of the box `esm` just works, no configuration necessary, and supports:
59
60* Passing all applicable [test262](https://github.com/tc39/test262) compliance tests
61* [`import`](https://ponyfoo.com/articles/es6-modules-in-depth#import)/[`export`](https://ponyfoo.com/articles/es6-modules-in-depth#export)
62* [`import.meta`](https://github.com/tc39/proposal-import-meta)
63* [Dynamic `import`](https://github.com/tc39/proposal-dynamic-import)
64* [Live bindings](https://ponyfoo.com/articles/es6-modules-in-depth#bindings-not-values)
65* [File URI scheme](https://en.wikipedia.org/wiki/File_URI_scheme)
66* Node `stdin`, [`--eval`](https://nodejs.org/api/cli.html#cli_e_eval_script), [`--print`](https://nodejs.org/api/cli.html#cli_p_print_script) flags
67* Node [`--check`](https://nodejs.org/api/cli.html#cli_c_check) flag _(Node 10+)_
68
69Options
70---
71
72Specify options with one of the following:
73
74* `"esm"` field in `package.json`
75* CJS/ESM in an `.esmrc.js`, `.esmrc.cjs`, or `.esmrc.mjs` file
76* [JSON6](https://github.com/d3x0r/json6) in an `.esmrc` or `.esmrc.json` file
77* JSON6 or file path in the `ESM_OPTIONS` environment variable
78* `ESM_DISABLE_CACHE` environment variable
79
80<table><tr><td colspan=2><code>{</code><tr><td valign=top><code>"cjs":true</code><td><p>A boolean or object for toggling CJS features in ESM.<details><summary>Features</summary><table><tr><td colspan=2><code>{</code><tr><td valign=top><code>"cache":true</code><td><p>A boolean for storing ES modules in <code>require.cache</code>.<tr><td valign=top><code>"esModule":true</code><td><p>A boolean for <code>__esModule</code> interoperability.<tr><td valign=top><code>"extensions":true</code><td><p>A boolean for respecting <code>require.extensions</code> in ESM.<tr><td valign=top><code>"mutableNamespace":true</code><td><p>A boolean for mutable <a href=https://ponyfoo.com/articles/es6-modules-in-depth#import-all-the-things>namespace objects</a>.<tr><td valign=top><code>"namedExports":true</code><td><p>A boolean for <a href=https://ponyfoo.com/articles/es6-modules-in-depth#importing-named-exports>importing named exports</a> of CJS modules.<tr><td valign=top><code>"paths":true</code><td><p>A boolean for following CJS <a href=https://github.com/nodejs/node-eps/blob/master/002-es-modules.md#432-removal-of-non-local-dependencies>path rules</a> in ESM.<tr><td valign=top><code>"vars":true</code><td><p>A boolean for <code>__dirname</code>, <code>__filename</code>, and <code>require</code> in ESM.<tr><td valign=top><code>"dedefault":false</code><td><p>A boolean for requiring ES modules without the dangling <code>require().default</code>.<tr><td valign=top><code>"topLevelReturn":false</code><td><p>A boolean for top-level <code>return</code> support.<tr><td colspan=2><code>}</code></table></details><tr><td valign=top><code>"mainFields":["main"]</code><td><p>An array of fields checked when importing a package.<tr><td valign=top><code>"mode":"auto"</code><td><p>A string mode:<ul><li><code>"auto"</code> detect files with <code>import</code>, <code>import.meta</code>, <code>export</code>,<br><a href=https://github.com/tc39/proposal-modules-pragma><code>"use module"</code></a>, or <code>.mjs</code> as ESM.<li><code>"all"</code> files besides those with <code>"use script"</code> or <code>.cjs</code> are treated as ESM.<li><code>"strict"</code> to treat <strong>only</strong> <code>.mjs</code> files as ESM.</ul><tr><td valign=top><code>"await":false</code><td><p>A boolean for <a href=https://github.com/tc39/proposal-top-level-await>top-level <code>await</code></a> in modules without ESM exports. <em>(Node 10+)</em><tr><td valign=top><code>"force":false</code><td><p>A boolean to apply these options to all module loads.<tr><td valign=top><code>"wasm":false</code><td><p>A boolean for <a href=https://nodejs.org/api/globals.html#globals_webassembly>WebAssembly</a> module support. <em>(Node 8+)</em><tr><td colspan=2><code>}</code></table>
81
82DevOpts
83---
84
85<table><tr><td colspan=2><code>{</code><tr><td valign=top><code>"cache":true</code><td><p>A boolean for toggling cache creation or a cache directory path.<tr><td valign=top><code>"sourceMap":false</code><td><p>A boolean for including inline source maps.<tr><td colspan=2><code>}</code></table>
86
87Tips
88---
89
90### Bundling
91
92* For bundlers like [`browserify`](http://browserify.org/)+[`esmify`](https://github.com/mattdesl/esmify),
93 [`parcel-bundler`](https://parceljs.org/), and [`webpack`](https://webpack.js.org/)
94 add a `"module"` field to `package.json` pointing to the main ES module.
95 ```json
96 "main": "index.js",
97 "module": "main.js"
98 ```
99
100 :bulb: This is automagically done with `npm init esm` or `yarn create esm`.
101
102### Extensions
103
104* Enable `esm` for [`wallaby.js`](https://wallabyjs.com/) following their
105 [integration example](https://wallabyjs.com/docs/integration/node.html#es-modules).
106
107### Loading
108
109* Load `esm` before loaders/monitors like
110 [`@babel/register`](https://babeljs.io/docs/en/next/babel-register.html),
111 [`newrelic`](https://github.com/newrelic/node-newrelic),
112 [`sqreen`](https://docs.sqreen.io/sqreen-for-nodejs/getting-started-2/), and
113 [`ts-node`](https://github.com/TypeStrong/ts-node#programmatic).
114
115* Load `esm` for [`jasmine`](https://jasmine.github.io/) using the
116 [`"helpers"`](https://jasmine.github.io/setup/nodejs.html#configuration)
117 field in `jasmine.json`:
118 ```json
119 "helpers": [
120 "node_modules/esm"
121 ]
122 ```
123
124* Load `esm` with “node-args" options of:<br>
125 - [`pm2`](https://pm2.io/doc/en/runtime/reference/pm2-cli/#pm2-flags): `--node-args="-r esm"`
126
127* Load `esm` with “require” options of
128 [`ava`](https://github.com/avajs/ava/blob/master/docs/recipes/es-modules.md),
129 [`mocha`](https://mochajs.org/#-require-module-r-module),
130 [`nodemon`](https://nodemon.io/),
131 [`nyc`](https://github.com/istanbuljs/nyc#require-additional-modules),
132 [`qunit`](https://github.com/qunitjs/qunit/releases/tag/2.6.0),
133 [`tape`](https://github.com/substack/tape#preloading-modules), and
134 [`webpack`](https://webpack.js.org/api/cli/#config-options).
135
136 :bulb: Builtin `require` cannot sideload `.mjs` files. However, `.js` files
137 can be sideloaded or `.mjs` files may be loaded with dynamic `import`.
138
\No newline at end of file