ECMAScript modules are the official standard format to package JavaScript code for reuse. Modules are defined using a variety of import
and export
statements.
Node.js fully supports ECMAScript modules as they are currently specified and provides limited interoperability between them and the existing module format, CommonJS.
Node.js contains support for ES Modules based upon the Node.js EP for ES Modules and the ECMAScript-modules implementation.
Expect major changes in the implementation including interoperability support, specifier resolution, and default behavior.
The --experimental-modules
flag can be used to enable support for ECMAScript modules (ES modules).
Once enabled, Node.js will treat the following as ES modules when passed to node
as the initial input, or when referenced by import
statements within ES module code:
Files ending in .mjs
.
Files ending in .js
, or extensionless files, when the nearest parent package.json
file contains a top-level field "type"
with a value of "module"
.
Strings passed in as an argument to --eval
or --print
, or piped to node
via STDIN
, with the flag --input-type=module
.
Node.js will treat as CommonJS all other forms of input, such as .js
files where the nearest parent package.json
file contains no top-level "type"
field, or string input without the flag --input-type
. This behavior is to preserve backward compatibility. However, now that Node.js supports both CommonJS and ES modules, it is best to be explicit whenever possible. Node.js will treat the following as CommonJS when passed to node
as the initial input, or when referenced by import
statements within ES module code:
Files ending in .cjs
.
Files ending in .js
, or extensionless files, when the nearest parent package.json
file contains a top-level field "type"
with a value of "commonjs"
.
Strings passed in as an argument to --eval
or --print
, or piped to node
via STDIN
, with the flag --input-type=commonjs
.
package.json
"type"
fieldFiles ending with .js
or .mjs
, or lacking any extension, will be loaded as ES modules when the nearest parent package.json
file contains a top-level field "type"
with a value of "module"
.
The nearest parent package.json
is defined as the first package.json
found when searching in the current folder, that folder’s parent, and so on up until the root of the volume is reached.
// package.json { "type": "module" }
# In same folder as above package.json node --experimental-modules my-app.js # Runs as ES module
If the nearest parent package.json
lacks a "type"
field, or contains "type": "commonjs"
, extensionless and .js
files are treated as CommonJS. If the volume root is reached and no package.json
is found, Node.js defers to the default, a package.json
with no "type"
field.
import
statements of .js
and extensionless files are treated as ES modules if the nearest parent package.json
contains "type": "module"
.
// my-app.js, part of the same example as above import './startup.js'; // Loaded as ES module because of package.json
A folder containing a package.json
file, and all subfolders below that folder down until the next folder containing another package.json
, is considered a package scope. The "type"
field defines how .js
and extensionless files should be treated within a particular package.json
file’s package scope. Every package in a project’s node_modules
folder contains its own package.json
file, so each project’s dependencies have their own package scopes. A package.json
lacking a "type"
field is treated as if it contained "type": "commonjs"
.
The package scope applies not only to initial entry points (node --experimental-modules my-app.js
) but also to files referenced by import
statements and import()
expressions.
// my-app.js, in an ES module package scope because there is a package.json // file in the same folder with "type": "module". import './startup/init.js'; // Loaded as ES module since ./startup contains no package.json file, // and therefore inherits the ES module package scope from one level up. import 'commonjs-package'; // Loaded as CommonJS since ./node_modules/commonjs-package/package.json // lacks a "type" field or contains "type": "commonjs". import './node_modules/commonjs-package/index.js'; // Loaded as CommonJS since ./node_modules/commonjs-package/package.json // lacks a "type" field or contains "type": "commonjs".
Files ending with .mjs
are always loaded as ES modules regardless of package scope.
Files ending with .cjs
are always loaded as CommonJS regardless of package scope.
import './legacy-file.cjs'; // Loaded as CommonJS since .cjs is always loaded as CommonJS. import 'commonjs-package/src/index.mjs'; // Loaded as ES module since .mjs is always loaded as ES module.
The .mjs
and .cjs
extensions may be used to mix types within the same package scope:
Within a "type": "module"
package scope, Node.js can be instructed to interpret a particular file as CommonJS by naming it with a .cjs
extension (since both .js
and .mjs
files are treated as ES modules within a "module"
package scope).
Within a "type": "commonjs"
package scope, Node.js can be instructed to interpret a particular file as an ES module by naming it with an .mjs
extension (since both .js
and .cjs
files are treated as CommonJS within a "commonjs"
package scope).
--input-type
flagStrings passed in as an argument to --eval
or --print
(or -e
or -p
), or piped to node
via STDIN
, will be treated as ES modules when the --input-type=module
flag is set.
node --experimental-modules --input-type=module --eval \ "import { sep } from 'path'; console.log(sep);" echo "import { sep } from 'path'; console.log(sep);" | \ node --experimental-modules --input-type=module
For completeness there is also --input-type=commonjs
, for explicitly running string input as CommonJS. This is the default behavior if --input-type
is unspecified.
The package.json
"main"
field defines the entry point for a package, whether the package is included into CommonJS via require
or into an ES module via import
.
// ./node_modules/es-module-package/package.json { "type": "module", "main": "./src/index.js" }
// ./my-app.mjs import { something } from 'es-module-package'; // Loads from ./node_modules/es-module-package/src/index.js
An attempt to require
the above es-module-package
would attempt to load ./node_modules/es-module-package/src/index.js
as CommonJS, which would throw an error as Node.js would not be able to parse the export
statement in CommonJS.
As with import
statements, for ES module usage the value of "main"
must be a full path including extension: "./index.mjs"
, not "./index"
.
If the package.json
"type"
field is omitted, a .js
file in "main"
will be interpreted as CommonJS.
The "main"
field can point to exactly one file, regardless of whether the package is referenced via require
(in a CommonJS context) or import
(in an ES module context). Package authors who want to publish a package to be used in both contexts can do so by setting "main"
to point to the CommonJS entry point and informing the package’s users of the path to the ES module entry point. Such a package would be accessible like require('pkg')
and import 'pkg/module.mjs'
. Alternatively the package "main"
could point to the ES module entry point and legacy users could be informed of the CommonJS entry point path, e.g. require('pkg/commonjs')
.
By default, all subpaths from a package can be imported (import 'pkg/x.js'
). Custom subpath aliasing and encapsulation can be provided through the "exports"
field.
// ./node_modules/es-module-package/package.json { "exports": { "./submodule": "./src/submodule.js" } }
import submodule from 'es-module-package/submodule'; // Loads ./node_modules/es-module-package/src/submodule.js
In addition to defining an alias, subpaths not defined by "exports"
will throw when an attempt is made to import them:
import submodule from 'es-module-package/private-module.js'; // Throws - Module not found
Note: this is not a strong encapsulation as any private modules can still be loaded by absolute paths.
Folders can also be mapped with package exports:
// ./node_modules/es-module-package/package.json { "exports": { "./features/": "./src/features/" } }
import feature from 'es-module-package/features/x.js'; // Loads ./node_modules/es-module-package/src/features/x.js
If a package has no exports, setting "exports": false
can be used instead of "exports": {}
to indicate the package does not intend for submodules to be exposed.
Any invalid exports entries will be ignored. This includes exports not starting with "./"
or a missing trailing "/"
for directory exports.
Array fallback support is provided for exports, similarly to import maps in order to be forward-compatible with fallback workflows in future:
{ "exports": { "./submodule": ["not:valid", "./submodule.js"] } }
Since "not:valid"
is not a supported target, "./submodule.js"
is used instead as the fallback, as if it were the only target.
import
SpecifiersThe specifier of an import
statement is the string after the from
keyword, e.g. 'path'
in import { sep } from 'path'
. Specifiers are also used in export from
statements, and as the argument to an import()
expression.
There are four types of specifiers:
Bare specifiers like 'some-package'
. They refer to an entry point of a package by the package name.
Deep import specifiers like 'some-package/lib/shuffle.mjs'
. They refer to a path within a package prefixed by the package name.
Relative specifiers like './startup.js'
or '../config.mjs'
. They refer to a path relative to the location of the importing file.
Absolute specifiers like 'file:///opt/nodejs/config.js'
. They refer directly and explicitly to a full path.
Bare specifiers, and the bare specifier portion of deep import specifiers, are strings; but everything else in a specifier is a URL.
Only file://
URLs are supported. A specifier like 'https://example.com/app.js'
may be supported by browsers but it is not supported in Node.js.
Specifiers may not begin with /
or //
. These are reserved for potential future use. The root of the current volume may be referenced via file:///
.
The import.meta
metaproperty is an Object
that contains the following property:
url
<string> The absolute file:
URL of the module.A file extension must be provided when using the import
keyword. Directory indexes (e.g. './startup/index.js'
) must also be fully specified.
This behavior matches how import
behaves in browser environments, assuming a typically configured server.
NODE_PATH
NODE_PATH
is not part of resolving import
specifiers. Please use symlinks if this behavior is desired.
require
, exports
, module.exports
, __filename
, __dirname
These CommonJS variables are not available in ES modules.
require
can be imported into an ES module using module.createRequire()
.
Equivalents of __filename
and __dirname
can be created inside of each file via import.meta.url
.
import { fileURLToPath } from 'url'; import { dirname } from 'path'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename);
require.extensions
require.extensions
is not used by import
. The expectation is that loader hooks can provide this workflow in the future.
require.cache
require.cache
is not used by import
. It has a separate cache.
ES modules are resolved and cached based upon URL semantics. This means that files containing special characters such as #
and ?
need to be escaped.
Modules will be loaded multiple times if the import
specifier used to resolve them have a different query or fragment.
import './foo.mjs?query=1'; // loads ./foo.mjs with query of "?query=1" import './foo.mjs?query=2'; // loads ./foo.mjs with query of "?query=2"
For now, only modules using the file:
protocol can be loaded.
require
require
always treats the files it references as CommonJS. This applies whether require
is used the traditional way within a CommonJS environment, or in an ES module environment using module.createRequire()
.
To include an ES module into CommonJS, use import()
.
import
statementsAn import
statement can reference an ES module, a CommonJS module, or JSON. Other file types such as Native modules are not supported. For those, use module.createRequire()
.
import
statements are permitted only in ES modules. For similar functionality in CommonJS, see import()
.
The specifier of an import
statement (the string after the from
keyword) can either be an URL-style relative path like './file.mjs'
or a package name like 'fs'
.
Like in CommonJS, files within packages can be accessed by appending a path to the package name.
import { sin, cos } from 'geometry/trigonometry-functions.mjs';
Currently only the “default export” is supported for CommonJS files or packages:
import packageMain from 'commonjs-package'; // Works import { method } from 'commonjs-package'; // ErrorsThere are ongoing efforts to make the latter code possible.
import()
expressionsDynamic import()
is supported in both CommonJS and ES modules. It can be used to include ES module files from CommonJS code.
(async () => { await import('./my-app.mjs'); })();
CommonJS, JSON, and Native modules can be used with module.createRequire()
.
// cjs.js module.exports = 'cjs'; // esm.mjs import { createRequire } from 'module'; import { fileURLToPath as fromURL } from 'url'; const require = createRequire(fromURL(import.meta.url)); const cjs = require('./cjs'); cjs === 'cjs'; // true
Builtin modules will provide named exports of their public API, as well as a default export which can be used for, among other things, modifying the named exports. Named exports of builtin modules are updated when the corresponding exports property is accessed, redefined, or deleted.
import EventEmitter from 'events'; const e = new EventEmitter();
import { readFile } from 'fs'; readFile('./foo.txt', (err, source) => { if (err) { console.error(err); } else { console.log(source); } });
import fs, { readFileSync } from 'fs'; fs.readFileSync = () => Buffer.from('Hello, ESM'); fs.readFileSync === readFileSync;
JSON modules follow the WHATWG JSON modules specification.
The imported JSON only exposes a default
. There is no support for named exports. A cache entry is created in the CommonJS cache, to avoid duplication. The same object will be returned in CommonJS if the JSON module has already been imported from the same path.
Assuming an index.mjs
with
import packageConfig from './package.json';
Importing Web Assembly modules is supported under the --experimental-wasm-modules
flag, allowing any .wasm
files to be imported as normal modules while also supporting their module imports.
This integration is in line with the ES Module Integration Proposal for Web Assembly.
For example, an index.mjs
containing:
import * as M from './module.wasm'; console.log(M);
executed under:
node --experimental-modules --experimental-wasm-modules index.mjs
would provide the exports interface for the instantiation of module.wasm
.
Note: This API is currently being redesigned and will still change.
To customize the default module resolution, loader hooks can optionally be provided via a --loader ./loader-name.mjs
argument to Node.js.
When hooks are used they only apply to ES module loading and not to any CommonJS modules loaded.
The resolve hook returns the resolved file URL and module format for a given module specifier and parent file URL:
const baseURL = new URL(`${process.cwd()}/`, 'file://'); export async function resolve(specifier, parentModuleURL = baseURL, defaultResolver) { return { url: new URL(specifier, parentModuleURL).href, format: 'module' }; }
The parentModuleURL
is provided as undefined
when performing main Node.js load itself.
The default Node.js ES module resolution function is provided as a third argument to the resolver for easy compatibility workflows.
In addition to returning the resolved file URL value, the resolve hook also returns a format
property specifying the module format of the resolved module. This can be one of the following:
format |
Description |
---|---|
'builtin' |
Load a Node.js builtin module |
'commonjs' |
Load a Node.js CommonJS module |
'dynamic' |
Use a dynamic instantiate hook |
'json' |
Load a JSON file |
'module' |
Load a standard JavaScript module |
'wasm' |
Load a WebAssembly module |
For example, a dummy loader to load JavaScript restricted to browser resolution rules with only JS file extension and Node.js builtin modules support could be written:
import path from 'path'; import process from 'process'; import Module from 'module'; const builtins = Module.builtinModules; const JS_EXTENSIONS = new Set(['.js', '.mjs']); const baseURL = new URL(`${process.cwd()}/`, 'file://'); export function resolve(specifier, parentModuleURL = baseURL, defaultResolve) { if (builtins.includes(specifier)) { return { url: specifier, format: 'builtin' }; } if (/^\.{0,2}[/]/.test(specifier) !== true && !specifier.startsWith('file:')) { // For node_modules support: // return defaultResolve(specifier, parentModuleURL); throw new Error( `imports must begin with '/', './', or '../'; '${specifier}' does not`); } const resolved = new URL(specifier, parentModuleURL); const ext = path.extname(resolved.pathname); if (!JS_EXTENSIONS.has(ext)) { throw new Error( `Cannot load file with non-JavaScript file extension ${ext}.`); } return { url: resolved.href, format: 'module' }; }
With this loader, running:
NODE_OPTIONS='--experimental-modules --loader ./custom-loader.mjs' node x.js
would load the module x.js
as an ES module with relative resolution support (with node_modules
loading skipped in this example).
To create a custom dynamic module that doesn't correspond to one of the existing format
interpretations, the dynamicInstantiate
hook can be used. This hook is called only for modules that return format: 'dynamic'
from the resolve
hook.
export async function dynamicInstantiate(url) { return { exports: ['customExportName'], execute: (exports) => { // Get and set functions provided for pre-allocated export names exports.customExportName.set('value'); } }; }
With the list of module exports provided upfront, the execute
function will then be called at the exact point of module evaluation order for that module in the import tree.
The resolver has the following properties:
The algorithm to load an ES module specifier is given through the ESM_RESOLVE method below. It returns the resolved URL for a module specifier relative to a parentURL, in addition to the unique module format for that resolved URL given by the ESM_FORMAT routine.
The "module" format is returned for an ECMAScript Module, while the "commonjs" format is used to indicate loading through the legacy CommonJS loader. Additional formats such as "addon" can be extended in future updates.
In the following algorithms, all subroutine errors are propagated as errors of these top-level routines unless stated otherwise.
isMain is true when resolving the Node.js application entry point.
ESM_RESOLVE(specifier, parentURL, isMain)
- Let resolvedURL be undefined.
If specifier is a valid URL, then
- Set resolvedURL to the result of parsing and reserializing specifier as a URL.
Otherwise, if specifier starts with "/", then
- Throw an Invalid Specifier error.
Otherwise, if specifier starts with "./" or "../", then
- Set resolvedURL to the URL resolution of specifier relative to parentURL.
Otherwise,
- Note: specifier is now a bare specifier.
- Set resolvedURL the result of PACKAGE_RESOLVE(specifier, parentURL).
If resolvedURL contains any percent encodings of "/" or "\" ("%2f" and "%5C" respectively), then
- Throw an Invalid Specifier error.
If the file at resolvedURL does not exist, then
- Throw a Module Not Found error.
- Set resolvedURL to the real path of resolvedURL.
- Let format be the result of ESM_FORMAT(resolvedURL, isMain).
- Load resolvedURL as module format, format.
PACKAGE_RESOLVE(packageSpecifier, parentURL)
- Let packageName be undefined.
- Let packageSubpath be undefined.
If packageSpecifier is an empty string, then
- Throw an Invalid Specifier error.
If packageSpecifier does not start with "@", then
- Set packageName to the substring of packageSpecifier until the first "/" separator or the end of the string.
Otherwise,
If packageSpecifier does not contain a "/" separator, then
- Throw an Invalid Specifier error.
- Set packageName to the substring of packageSpecifier until the second "/" separator or the end of the string.
If packageName starts with "." or contains "\" or "%", then
- Throw an Invalid Specifier error.
- Let packageSubpath be undefined.
If the length of packageSpecifier is greater than the length of packageName, then
- Set packageSubpath to "." concatenated with the substring of packageSpecifier from the position at the length of packageName.
If packageSubpath contains any "." or ".." segments or percent encoded strings for "/" or "\" then,
- Throw an Invalid Specifier error.
If packageSubpath is undefined and packageName is a Node.js builtin module, then
- Return the string "node:" concatenated with packageSpecifier.
While parentURL is not the file system root,
- Let packageURL be the URL resolution of "node_modules/" concatenated with packageSpecifier, relative to parentURL.
- Set parentURL to the parent folder URL of parentURL.
If the folder at packageURL does not exist, then
- Set parentURL to the parent URL path of parentURL.
- Continue the next loop iteration.
- Let pjson be the result of READ_PACKAGE_JSON(packageURL).
If packageSubpath is _undefined__, then
- Return the result of PACKAGE_MAIN_RESOLVE(packageURL, pjson).
Otherwise,
If pjson is not null and pjson has an "exports" key, then
- Let exports be pjson.exports.
If exports is not null or undefined, then
- Return PACKAGE_EXPORTS_RESOLVE(packageURL, packageSubpath, pjson.exports).
- Return the URL resolution of packageSubpath in packageURL.
- Throw a Module Not Found error.
PACKAGE_MAIN_RESOLVE(packageURL, pjson)
If pjson is null, then
- Throw a Module Not Found error.
If pjson.main is a String, then
- Let resolvedMain be the URL resolution of packageURL, "/", and pjson.main.
If the file at resolvedMain exists, then
- Return resolvedMain.
If pjson.type is equal to "module", then
- Throw a Module Not Found error.
- Let legacyMainURL be the result applying the legacy LOAD_AS_DIRECTORY CommonJS resolver to packageURL, throwing a Module Not Found error for no resolution.
- Return legacyMainURL.
PACKAGE_EXPORTS_RESOLVE(packageURL, packagePath, exports)
If exports is an Object, then
- Set packagePath to "./" concatenated with packagePath.
If packagePath is a key of exports, then
- Let target be the value of exports[packagePath].
- Return PACKAGE_EXPORTS_TARGET_RESOLVE(packageURL, target, "").
- Let directoryKeys be the list of keys of exports ending in "/", sorted by length descending.
For each key directory in directoryKeys, do
If packagePath starts with directory, then
- Let target be the value of exports[directory].
- Let subpath be the substring of target starting at the index of the length of directory.
- Return PACKAGE_EXPORTS_TARGET_RESOLVE(packageURL, target, subpath).
- Throw a Module Not Found error.
PACKAGE_EXPORTS_TARGET_RESOLVE(packageURL, target, subpath)
If target is a String, then
- If target does not start with "./", throw a Module Not Found error.
- If subpath has non-zero length and target does not end with "/", throw a Module Not Found error.
- If target or subpath contain any "node_modules" segments including "node_modules" percent-encoding, throw a Module Not Found error.
- Let resolvedTarget be the URL resolution of the concatenation of packageURL and target.
If resolvedTarget is contained in packageURL, then
- Let resolved be the URL resolution of the concatenation of subpath and resolvedTarget.
If resolved is contained in resolvedTarget, then
- Return resolved.
Otherwise, if target is an Array, then
For each item targetValue in target, do
- If targetValue is not a String, continue the loop.
- Let resolved be the result of PACKAGE_EXPORTS_TARGET_RESOLVE(packageURL, targetValue, subpath), continuing the loop on abrupt completion.
- Assert: resolved is a String.
- Return resolved.
- Throw a Module Not Found error.
ESM_FORMAT(url, isMain)
- Assert: url corresponds to an existing file.
- Let pjson be the result of READ_PACKAGE_SCOPE(url).
If url ends in ".mjs", then
- Return "module".
If url ends in ".cjs", then
- Return "commonjs".
If pjson?.type exists and is "module", then
If isMain is true or url ends in ".js", then
- Return "module".
- Throw an Unsupported File Extension error.
Otherwise,
If isMain is true or url ends in ".js", ".json" or ".node", then
- Return "commonjs".
- Throw an Unsupported File Extension error.
READ_PACKAGE_SCOPE(url)
- Let scopeURL be url.
While scopeURL is not the file system root,
- If scopeURL ends in a "node_modules" path segment, return null.
- Let pjson be the result of READ_PACKAGE_JSON(scopeURL).
If pjson is not null, then
- Return pjson.
- Set scopeURL to the parent URL of scopeURL.
- Return null.
READ_PACKAGE_JSON(packageURL)
- Let pjsonURL be the resolution of "package.json" within packageURL.
If the file at pjsonURL does not exist, then
- Return null.
If the file at packageURL does not parse as valid JSON, then
- Throw an Invalid Package Configuration error.
- Return the parsed JSON source of the file at pjsonURL.
The current specifier resolution does not support all default behavior of the CommonJS loader. One of the behavior differences is automatic resolution of file extensions and the ability to import directories that have an index file.
The --es-module-specifier-resolution=[mode]
flag can be used to customize the extension resolution algorithm. The default mode is explicit
, which requires the full path to a module be provided to the loader. To enable the automatic extension resolution and importing from directories that include an index file use the node
mode.
$ node --experimental-modules index.mjs success! $ node --experimental-modules index #Failure! Error: Cannot find module $ node --experimental-modules --es-module-specifier-resolution=node index success!
© Joyent, Inc. and other Node contributors
Licensed under the MIT License.
Node.js is a trademark of Joyent, Inc. and is used with its permission.
We are not endorsed by or affiliated with Joyent.
https://nodejs.org/dist/latest-v12.x/docs/api/esm.html