@std/esm
This fast, small, zero dependency, package is all you need to enable
ES modules in Node 4+ today!
:book: See the release post
for all the details.
Getting started
-
Run npm i --save @std/esm
, with npm 2+,
in your app or package directory.
-
Call require("@std/esm")
before importing ES modules.
index.js
require("@std/esm")
module.exports = require("./main.mjs").default
For package authors with sub modules:
require("foo")
const bar = require("foo/bar").default
Enable ESM in the Node CLI by loading @std/esm
with the -r
option:
node -r @std/esm file.mjs
Enable ESM in the Node REPL by loading @std/esm
upon entering:
$ node
> require("@std/esm")
@std/esm enabled
> import p from "path"
undefined
> p.join("hello", "world")
'hello/world'
Note: The "cjs"
and "gz"
options are unlocked in the Node REPL.
Standard Features
The @std/esm
loader is as spec-compliant
as possible and follows Node’s rules.
:point_right: This means, by default, ESM requires the use of the .mjs
extension.
:unlock: You can unlock unambiguous .js
use with the "js"
option.
Out of the box @std/esm
just works, no configuration necessary, and supports:
Unlockables
Unlock extra features with "@std/esm":options
or
"@std":{"esm":options}
in your package.json.
Note: All options are off by default and may be specified as either an object or ESM mode string.
{
"@std/esm": {
|
"await": | A boolean for top-level await in the main ES module |
"gz": |
A boolean for gzipped module support (i.e. .js.gz , .mjs.gz )
|
"esm": |
A string ESM mode
"mjs" files as ESM (default)"all" files as ESM"js" files with import , export , or "use module" as ESM
|
"cjs": |
A boolean for CJS features in ESM
|
}
}
|