esm
DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/esm package

3.2.25 • Public • Published

esm

The brilliantly simple, babel-less, bundle-less ECMAScript module loader.

esm is the world’s most advanced ECMAScript module loader. This fast, production ready, zero dependency loader is all you need to support ECMAScript modules in Node 6+. See the release post and video for details!

Install

  • New projects

    Run npm init esm or yarn create esm.

    💡 Use the -y flag to answer “yes” to all prompts.

  • Existing projects

    Run npm i esm or yarn add esm.

Getting started

There are two ways to enable esm.

  1. Enable esm for packages:

    Use esm to load the main ES module and export it as CommonJS.

    index.js

    // Set options as a parameter, environment variable, or rc file.
    require = require("esm")(module/*, options*/)
    module.exports = require("./main.js")

    main.js

    // ESM syntax is supported.
    export {}

    💡 These files are automagically created with npm init esm or yarn create esm.

  2. Enable esm for local runs:

    node -r esm main.js

    💡 Omit the filename to enable esm in the REPL.

Features

👏 By default, 💯 percent CJS interoperability is enabled so you can get stuff done.
🔒 .mjs files are limited to basic functionality without support for esm options.

Out of the box esm just works, no configuration necessary, and supports:

Options

Specify options with one of the following:

  • "esm" field in package.json
  • CJS/ESM in an .esmrc.js, .esmrc.cjs, or .esmrc.mjs file
  • JSON6 in an .esmrc or .esmrc.json file
  • JSON6 or file path in the ESM_OPTIONS environment variable
  • ESM_DISABLE_CACHE environment variable

{
"cjs":true

A boolean or object for toggling CJS features in ESM.

Features

{
"cache":true

A boolean for storing ES modules in require.cache.

"esModule":true

A boolean for __esModule interoperability.

"extensions":true

A boolean for respecting require.extensions in ESM.

"mutableNamespace":true

A boolean for mutable namespace objects.

"namedExports":true

A boolean for importing named exports of CJS modules.

"paths":true

A boolean for following CJS path rules in ESM.

"vars":true

A boolean for __dirname, __filename, and require in ESM.

"dedefault":false

A boolean for requiring ES modules without the dangling require().default.

"topLevelReturn":false

A boolean for top-level return support.

}
"mainFields":["main"]

An array of fields checked when importing a package.

"mode":"auto"

A string mode:

  • "auto" detect files with import, import.meta, export,
    "use module", or .mjs as ESM.
  • "all" files besides those with "use script" or .cjs are treated as ESM.
  • "strict" to treat only .mjs files as ESM.
"await":false

A boolean for top-level await in modules without ESM exports. (Node 10+)

"force":false

A boolean to apply these options to all module loads.

"wasm":false

A boolean for WebAssembly module support. (Node 8+)

}

DevOpts

{
"cache":true

A boolean for toggling cache creation or a cache directory path.

"sourceMap":false

A boolean for including inline source maps.

}

Tips

Bundling

  • For bundlers like browserify+esmify, parcel-bundler, and webpack add a "module" field to package.json pointing to the main ES module.

    "main""index.js",
    "module""main.js"

    💡 This is automagically done with npm init esm or yarn create esm.

Extensions

Loading

  • Load esm before loaders/monitors like @babel/register, newrelic, sqreen, and ts-node.

  • Load esm for jasmine using the "helpers" field in jasmine.json:

    "helpers"[
      "node_modules/esm"
    ]
  • Load esm with “node-args" options of:

    • pm2: --node-args="-r esm"
  • Load esm with “require” options of ava, mocha, nodemon, nyc, qunit, tape, and webpack.

    💡 Builtin require cannot sideload .mjs files. However, .js files can be sideloaded or .mjs files may be loaded with dynamic import.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
3.2.251,736,358latest

Version History

VersionDownloads (Last 7 Days)Published
3.2.251,736,358
3.2.24948
3.2.2362
3.2.22999
3.2.2116
3.2.20467
3.2.193
3.2.18637
3.2.1763
3.2.167
3.2.154
3.2.1411
3.2.1337
3.2.124
3.2.1129
3.2.1013
3.2.994
3.2.84
3.2.7130
3.2.6196
3.2.55,099
3.2.491
3.2.321
3.2.21
3.2.112
3.2.021
3.1.45,938
3.1.39
3.1.23
3.1.194
3.1.0665
3.0.843,798
3.0.832
3.0.8210
3.0.817
3.0.804
3.0.793
3.0.782
3.0.775
3.0.7668
3.0.753
3.0.7431
3.0.731
3.0.7216
3.0.715
3.0.703
3.0.692
3.0.681
3.0.675
3.0.666
3.0.652
3.0.641
3.0.637
3.0.625
3.0.615
3.0.605
3.0.593
3.0.585
3.0.561
3.0.574
3.0.554
3.0.542
3.0.534
3.0.523
3.0.519
3.0.501
3.0.493
3.0.486
3.0.472
3.0.466
3.0.455
3.0.443
3.0.433
3.0.427
3.0.411
3.0.400
3.0.392
3.0.381
3.0.374
3.0.3612
3.0.352
3.0.3452
3.0.332
3.0.322
3.0.313
3.0.304
3.0.299
3.0.2830
3.0.273
3.0.263
3.0.253
3.0.246
3.0.233
3.0.223
3.0.212
3.0.202
3.0.193
3.0.181
3.0.176
3.0.163
3.0.155
3.0.142
3.0.130
3.0.122
3.0.112
3.0.102
3.0.91
3.0.81
3.0.72
3.0.64
3.0.51
3.0.46
3.0.30
3.0.25
3.0.10
3.0.01
2.2.02
2.1.21
2.1.13
2.1.05
2.0.94
2.0.81
2.0.70
2.0.63
2.0.52
2.0.45
2.0.32
2.0.26
2.0.15
2.0.00
1.0.91
1.0.80
1.0.73
1.0.62
1.0.51
1.0.43
1.0.30
1.0.21
1.0.11
1.0.00

Package Sidebar

Install

npm i esm

Weekly Downloads

1,756,351

Version

3.2.25

License

MIT

Unpacked Size

309 kB

Total Files

6

Last publish

Collaborators

  • jdalton