This fast, small, zero dependency, package is all you need to enable ES modules in Node 4+ today!
📖 See the release post for all the details.
-
Run
npm i --save @std/esmin your app or package directory. -
Add
.esm-cacheto your.gitignore. -
Create the ESM loader to import your main ES module:
index.js
require = require("@std/esm")(module) module.exports = require("./main.mjs").default
By default,
@std/esmonly processes files of packages that opt-in with a@std/esmoptions object or@std/esmas a dependency, dev dependency, or peer dependency in their package.json. However, you can enable processing all files with specific options by passing an options object as the second argument or passingtrueto use the options from your package.json.const loader1 = require("@std/esm")(module, { cjs: true, esm: "js" }) const loader2 = require("@std/esm")(module, true)
Enable ESM in the Node CLI by loading @std/esm with the -r option:
node -r @std/esm file.mjsEnable 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.
The @std/esm loader is as spec-compliant
as possible and follows Node’s rules.
👉 This means, by default, ESM requires the use of the .mjs file
extension.
🔓 You can unlock ESM with the .js file extension using
the "js" ESM mode.
Out of the box @std/esm just works, no configuration necessary, and supports:
import/export- Dynamic
import() - Live bindings
- Loading
.mjsfiles as ESM - The file URI scheme
- Node 4+ support
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.
|
|
"esm": |
A string ESM mode
|
"cjs": |
A boolean for CJS features in ESM
|
"await": |
A boolean for top-level |
"gz": |
A boolean for gzipped module support (i.e.
|
|
|