- Assertion testing
- Asynchronous context tracking
- Async hooks
- Buffer
- C++ addons
- C/C++ addons with Node-API
- C++ embedder API
- Child processes
- Cluster
- Command-line options
- Console
- Corepack
- Crypto
- Debugger
- Deprecated APIs
- Diagnostics Channel
- DNS
- Domain
- Errors
- Events
- File system
- Globals
- HTTP
- HTTP/2
- HTTPS
- Inspector
- Internationalization
- Modules: CommonJS modules
- Modules: ECMAScript modules
- Modules:
node:moduleAPI - Modules: Packages
- Net
- OS
- Path
- Performance hooks
- Permissions
- Process
- Punycode
- Query strings
- Readline
- REPL
- Report
- Single executable applications
- Stream
- String decoder
- Test runner
- Timers
- TLS/SSL
- Trace events
- TTY
- UDP/datagram
- URL
- Utilities
- V8
- VM
- WASI
- Web Crypto API
- Web Streams API
- Worker threads
- Zlib
Node.js v18.20.0 documentation
- Node.js v18.20.0
- ► Table of contents
-
►
Index
- Assertion testing
- Asynchronous context tracking
- Async hooks
- Buffer
- C++ addons
- C/C++ addons with Node-API
- C++ embedder API
- Child processes
- Cluster
- Command-line options
- Console
- Corepack
- Crypto
- Debugger
- Deprecated APIs
- Diagnostics Channel
- DNS
- Domain
- Errors
- Events
- File system
- Globals
- HTTP
- HTTP/2
- HTTPS
- Inspector
- Internationalization
- Modules: CommonJS modules
- Modules: ECMAScript modules
- Modules:
node:moduleAPI - Modules: Packages
- Net
- OS
- Path
- Performance hooks
- Permissions
- Process
- Punycode
- Query strings
- Readline
- REPL
- Report
- Single executable applications
- Stream
- String decoder
- Test runner
- Timers
- TLS/SSL
- Trace events
- TTY
- UDP/datagram
- URL
- Utilities
- V8
- VM
- WASI
- Web Crypto API
- Web Streams API
- Worker threads
- Zlib
- ► Other versions
- ► Options
Table of contents
Modules: ECMAScript modules#
Introduction#
ECMAScript modules are the official standard format to package JavaScript
code for reuse. Modules are defined using a variety of import and
export statements.
The following example of an ES module exports a function:
// addTwo.mjs
function addTwo(num) {
return num + 2;
}
export { addTwo };
The following example of an ES module imports the function from addTwo.mjs:
// app.mjs
import { addTwo } from './addTwo.mjs';
// Prints: 6
console.log(addTwo(4));
Node.js fully supports ECMAScript modules as they are currently specified and provides interoperability between them and its original module format, CommonJS.
Enabling#
Node.js has two module systems: CommonJS modules and ECMAScript modules.
Authors can tell Node.js to use the ECMAScript modules loader via the .mjs
file extension, the package.json "type" field, the --input-type
flag, or the --experimental-default-type flag. Outside of those cases,
Node.js will use the CommonJS module loader. See Determining module system
for more details.
Packages#
This section was moved to Modules: Packages.
import Specifiers#
Terminology#
The specifier of an import statement is the string after the from keyword,
e.g. 'node:path' in import { sep } from 'node:path'. Specifiers are also
used in export from statements, and as the argument to an import()
expression.
There are three types of specifiers:
-
Relative specifiers like
'./startup.js'or'../config.mjs'. They refer to a path relative to the location of the importing file. The file extension is always necessary for these. -
Bare specifiers like
'some-package'or'some-package/shuffle'. They can refer to the main entry point of a package by the package name, or a specific feature module within a package prefixed by the package name as per the examples respectively. Including the file extension is only necessary for packages without an"exports"field. -
Absolute specifiers like
'file:///opt/nodejs/config.js'. They refer directly and explicitly to a full path.
Bare specifier resolutions are handled by the Node.js module resolution and loading algorithm. All other specifier resolutions are always only resolved with the standard relative URL resolution semantics.
Like in CommonJS, module files within packages can be accessed by appending a
path to the package name unless the package's package.json contains an
"exports" field, in which case files within packages can only be accessed
via the paths defined in "exports".
For details on these package resolution rules that apply to bare specifiers in the Node.js module resolution, see the packages documentation.
Mandatory file extensions#
A file extension must be provided when using the import keyword to resolve
relative or absolute specifiers. 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.
URLs#
ES modules are resolved and cached as URLs. This means that special characters
must be percent-encoded, such as # with %23 and ? with %3F.
file:, node:, and data: URL schemes are supported. A specifier like
'https://example.com/app.js' is not supported natively in Node.js unless using
a custom HTTPS loader.
file: URLs#
Modules are loaded multiple times if the import specifier used to resolve
them has 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"
The volume root may be referenced via /, //, or file:///. Given the
differences between URL and path resolution (such as percent encoding
details), it is recommended to use url.pathToFileURL when importing a path.
data: imports#
data: URLs are supported for importing with the following MIME types:
text/javascriptfor ES modulesapplication/jsonfor JSONapplication/wasmfor Wasm
import 'data:text/javascript,console.log("hello!");';
import _ from 'data:application/json,"world!"' with { type: 'json' };
data: URLs only resolve bare specifiers for builtin modules
and absolute specifiers. Resolving
relative specifiers does not work because data: is not a
special scheme. For example, attempting to load ./foo
from data:text/javascript,import "./foo"; fails to resolve because there
is no concept of relative resolution for data: URLs.
node: imports#
node: URLs are supported as an alternative means to load Node.js builtin
modules. This URL scheme allows for builtin modules to be referenced by valid
absolute URL strings.
import fs from 'node:fs/promises';
Import attributes#
This feature was previously named "Import assertions", and using the
assertkeyword instead ofwith. Any uses in code of the priorassertkeyword should be updated to usewithinstead.
The Import Attributes proposal adds an inline syntax for module import statements to pass on more information alongside the module specifier.
import fooData from './foo.json' with { type: 'json' };
const { default: barData } =
await import('./bar.json', { with: { type: 'json' } });
Node.js supports the following type values, for which the attribute is
mandatory:
Attribute type | Needed for |
|---|---|
'json' | JSON modules |
Builtin modules#
Core modules provide named exports of their public API. A
default export is also provided which is the value of the CommonJS exports.
The default export can be used for, among other things, modifying the named
exports. Named exports of builtin modules are updated only by calling
module.syncBuiltinESMExports().
import EventEmitter from 'node:events';
const e = new EventEmitter();
import { readFile } from 'node:fs';
readFile('./foo.txt', (err, source) => {
if (err) {
console.error(err);
} else {
console.log(source);
}
});
import fs, { readFileSync } from 'node:fs';
import { syncBuiltinESMExports } from 'node:module';
import { Buffer } from 'node:buffer';
fs.readFileSync = () => Buffer.from('Hello, ESM');
syncBuiltinESMExports();
fs.readFileSync === readFileSync;
import() expressions#
Dynamic import() is supported in both CommonJS and ES modules. In CommonJS
modules it can be used to load ES modules.
import.meta#
The import.meta meta property is an Object that contains the following
properties.
import.meta.url#
- <string> The absolute
file:URL of the module.
This is defined exactly the same as it is in browsers providing the URL of the current module file.
This enables useful patterns such as relative file loading:
import { readFileSync } from 'node:fs';
const buffer = readFileSync(new URL('./data.proto', import.meta.url));
import.meta.resolve(specifier)#
specifier<string> The module specifier to resolve relative to the current module.- Returns: <string> The absolute URL string that the specifier would resolve to.
import.meta.resolve is a module-relative resolution function scoped to
each module, returning the URL string.
const dependencyAsset = import.meta.resolve('component-lib/asset.css');
// file:///app/node_modules/component-lib/asset.css
import.meta.resolve('./dep.js');
// file:///app/dep.js
All features of the Node.js module resolution are supported. Dependency resolutions are subject to the permitted exports resolutions within the package.
Caveats:
- This can result in synchronous file-system operations, which
can impact performance similarly to
require.resolve. - This feature is not available within custom loaders (it would create a deadlock).
Non-standard API:
When using the --experimental-import-meta-resolve flag, that function accepts
a second argument:
Interoperability with CommonJS#
import statements#
An import statement can reference an ES module or a CommonJS module.
import statements are permitted only in ES modules, but dynamic import()
expressions are supported in CommonJS for loading ES modules.
When importing CommonJS modules, the
module.exports object is provided as the default export. Named exports may be
available, provided by static analysis as a convenience for better ecosystem
compatibility.
require#
The CommonJS module require always treats the files it references as CommonJS.
Using require to load an ES module is not supported because ES modules have
asynchronous execution. Instead, use import() to load an ES module
from a CommonJS module.
CommonJS Namespaces#
CommonJS modules consist of a module.exports object which can be of any type.
When importing a CommonJS module, it can be reliably imported using the ES module default import or its corresponding sugar syntax:
import { default as cjs } from 'cjs';
// The following import statement is "syntax sugar" (equivalent but sweeter)
// for `{ default as cjsSugar }` in the above import statement:
import cjsSugar from 'cjs';
console.log(cjs);
console.log(cjs === cjsSugar);
// Prints:
// <module.exports>
// true
The ECMAScript Module Namespace representation of a CommonJS module is always
a namespace with a default export key pointing to the CommonJS
module.exports value.
This Module Namespace Exotic Object can be directly observed either when using
import * as m from 'cjs' or a dynamic import:
import * as m from 'cjs';
console.log(m);
console.log(m === await import('cjs'));
// Prints:
// [Module] { default: <module.exports> }
// true
For better compatibility with existing usage in the JS ecosystem, Node.js in addition attempts to determine the CommonJS named exports of every imported CommonJS module to provide them as separate ES module exports using a static analysis process.
For example, consider a CommonJS module written:
// cjs.cjs
exports.name = 'exported';
The preceding module supports named imports in ES modules:
import { name } from './cjs.cjs';
console.log(name);
// Prints: 'exported'
import cjs from './cjs.cjs';
console.log(cjs);
// Prints: { name: 'exported' }
import * as m from './cjs.cjs';
console.log(m);
// Prints: [Module] { default: { name: 'exported' }, name: 'exported' }
As can be seen from the last example of the Module Namespace Exotic Object being
logged, the name export is copied off of the module.exports object and set
directly on the ES module namespace when the module is imported.
Live binding updates or new exports added to module.exports are not detected
for these named exports.
The detection of named exports is based on common syntax patterns but does not always correctly detect named exports. In these cases, using the default import form described above can be a better option.
Named exports detection covers many common export patterns, reexport patterns and build tool and transpiler outputs. See cjs-module-lexer for the exact semantics implemented.
Differences between ES modules and CommonJS#
No require, exports, or module.exports#
In most cases, the ES module import can be used to load CommonJS modules.
If needed, a require function can be constructed within an ES module using
module.createRequire().
No __filename or __dirname#
These CommonJS variables are not available in ES modules.
__filename and __dirname use cases can be replicated via
import.meta.url.
No Addon Loading#
Addons are not currently supported with ES module imports.
They can instead be loaded with module.createRequire() or
process.dlopen.
No require.resolve#
Relative resolution can be handled via new URL('./local', import.meta.url).
For a complete require.resolve replacement, there is the
import.meta.resolve API.
Alternatively module.createRequire() can be used.
No NODE_PATH#
NODE_PATH is not part of resolving import specifiers. Please use symlinks
if this behavior is desired.
No require.extensions#
require.extensions is not used by import. Module customization hooks can
provide a replacement.
No require.cache#
require.cache is not used by import as the ES module loader has its own
separate cache.
JSON modules#
JSON files can be referenced by import:
import packageConfig from './package.json' with { type: 'json' };
The with { type: 'json' } syntax is mandatory; see Import Attributes.
The imported JSON only exposes a default export. There is no support for named
exports. A cache entry is created in the CommonJS cache to avoid duplication.
The same object is returned in CommonJS if the JSON module has already been
imported from the same path.
Wasm modules#
Importing WebAssembly 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 WebAssembly.
For example, an index.mjs containing:
import * as M from './module.wasm';
console.log(M);
executed under:
node --experimental-wasm-modules index.mjs
would provide the exports interface for the instantiation of module.wasm.
Top-level await#
The await keyword may be used in the top level body of an ECMAScript module.
Assuming an a.mjs with
export const five = await Promise.resolve(5);
And a b.mjs with
import { five } from './a.mjs';
console.log(five); // Logs `5`
node b.mjs # works
If a top level await expression never resolves, the node process will exit
with a 13 status code.
import { spawn } from 'node:child_process';
import { execPath } from 'node:process';
spawn(execPath, [
'--input-type=module',
'--eval',
// Never-resolving Promise:
'await new Promise(() => {})',
]).once('exit', (code) => {
console.log(code); // Logs `13`
});
HTTPS and HTTP imports#
Importing network based modules using https: and http: is supported under
the --experimental-network-imports flag. This allows web browser-like imports
to work in Node.js with a few differences due to application stability and
security concerns that are different when running in a privileged environment
instead of a browser sandbox.
Imports are limited to HTTP/1#
Automatic protocol negotiation for HTTP/2 and HTTP/3 is not yet supported.
HTTP is limited to loopback addresses#
http: is vulnerable to man-in-the-middle attacks and is not allowed to be
used for addresses outside of the IPv4 address 127.0.0.0/8 (127.0.0.1 to
127.255.255.255) and the IPv6 address ::1. Support for http: is intended
to be used for local development.
Authentication is never sent to the destination server.#
Authorization, Cookie, and Proxy-Authorization headers are not sent to the
server. Avoid including user info in parts of imported URLs. A security model
for safely using these on the server is being worked on.
CORS is never checked on the destination server#
CORS is designed to allow a server to limit the consumers of an API to a specific set of hosts. This is not supported as it does not make sense for a server-based implementation.
Cannot load non-network dependencies#
These modules cannot access other modules that are not over http: or https:.
To still access local modules while avoiding the security concern, pass in
references to the local dependencies:
// file.mjs
import worker_threads from 'node:worker_threads';
import { configure, resize } from 'https://example.com/imagelib.mjs';
configure({ worker_threads });
// https://example.com/imagelib.mjs
let worker_threads;
export function configure(opts) {
worker_threads = opts.worker_threads;
}
export function resize(img, size) {
// Perform resizing in worker_thread to avoid main thread blocking
}
Network-based loading is not enabled by default#
For now, the --experimental-network-imports flag is required to enable loading
resources over http: or https:. In the future, a different mechanism will be
used to enforce this. Opt-in is required to prevent transitive dependencies
inadvertently using potentially mutable state that could affect reliability
of Node.js applications.
Loaders#
The former Loaders documentation is now at Modules: Customization hooks.
Resolution and loading algorithm#
Features#
The default resolver has the following properties:
- FileURL-based resolution as is used by ES modules
- Relative and absolute URL resolution
- No default extensions
- No folder mains
- Bare specifier package resolution lookup through node_modules
- Does not fail on unknown extensions or protocols
- Can optionally provide a hint of the format to the loading phase
The default loader has the following properties
- Support for builtin module loading via
node:URLs - Support for "inline" module loading via
data:URLs - Support for
file:module loading - Fails on any other URL protocol
- Fails on unknown extensions for
file:loading (supports only.cjs,.js, and.mjs)
Resolution algorithm#
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.
The resolution algorithm determines the full resolved URL for a module
load, along with its suggested module format. The resolution algorithm
does not determine whether the resolved URL protocol can be loaded,
or whether the file extensions are permitted, instead these validations
are applied by Node.js during the load phase
(for example, if it was asked to load a URL that has a protocol that is
not file:, data:, node:, or if --experimental-network-imports
is enabled, https:).
The algorithm also tries to determine the format of the file based
on the extension (see ESM_FILE_FORMAT algorithm below). If it does
not recognize the file extension (eg if it is not .mjs, .cjs, or
.json), then a format of undefined is returned,
which will throw during the load phase.
The algorithm to determine the module format of a resolved URL is provided by ESM_FILE_FORMAT, which returns the unique module format for any file. 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.
defaultConditions is the conditional environment name array,
["node", "import"].
The resolver can throw the following errors:
- Invalid Module Specifier: Module specifier is an invalid URL, package name or package subpath specifier.
- Invalid Package Configuration: package.json configuration is invalid or contains an invalid configuration.
- Invalid Package Target: Package exports or imports define a target module for the package that is an invalid type or string target.
- Package Path Not Exported: Package exports do not define or permit a target subpath in the package for the given module.
- Package Import Not Defined: Package imports do not define the specifier.
- Module Not Found: The package or module requested does not exist.
- Unsupported Directory Import: The resolved path corresponds to a directory, which is not a supported target for module imports.
Resolution Algorithm Specification#
ESM_RESOLVE(specifier, parentURL)
- Let resolved be undefined.
- If specifier is a valid URL, then
- Set resolved to the result of parsing and reserializing specifier as a URL.
- Otherwise, if specifier starts with "/", "./", or "../", then
- Set resolved to the URL resolution of specifier relative to parentURL.
- Otherwise, if specifier starts with "#", then
- Set resolved to the result of PACKAGE_IMPORTS_RESOLVE(specifier, parentURL, defaultConditions).
- Otherwise,
- Note: specifier is now a bare specifier.
- Set resolved the result of PACKAGE_RESOLVE(specifier, parentURL).
- Let format be undefined.
- If resolved is a "file:" URL, then
- If resolved contains any percent encodings of "/" or "\" ("%2F" and "%5C" respectively), then
- Throw an Invalid Module Specifier error.
- If the file at resolved is a directory, then
- Throw an Unsupported Directory Import error.
- If the file at resolved does not exist, then
- Throw a Module Not Found error.
- Set resolved to the real path of resolved, maintaining the same URL querystring and fragment components.
- Set format to the result of ESM_FILE_FORMAT(resolved).
- Otherwise,
- Set format the module format of the content type associated with the URL resolved.
- Return format and resolved to the loading phase
PACKAGE_RESOLVE(packageSpecifier, parentURL)
- Let packageName be undefined.
- If packageSpecifier is an empty string, then
- Throw an Invalid Module Specifier error.
- If packageSpecifier is a Node.js builtin module name, then
- Return the string "node:" concatenated with packageSpecifier.
- 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 Module 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 Module Specifier error.
- Let packageSubpath be "." concatenated with the substring of packageSpecifier from the position at the length of packageName.
- If packageSubpath ends in "/", then
- Throw an Invalid Module Specifier error.
- Let selfUrl be the result of PACKAGE_SELF_RESOLVE(packageName, packageSubpath, parentURL).
- If selfUrl is not undefined, return selfUrl.
- 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
- Continue the next loop iteration.
- Let pjson be the result of READ_PACKAGE_JSON(packageURL).
- If pjson is not null and pjson.exports is not null or undefined, then
- Return the result of PACKAGE_EXPORTS_RESOLVE(packageURL, packageSubpath, pjson.exports, defaultConditions).
- Otherwise, if packageSubpath is equal to ".", then
- If pjson.main is a string, then
- Return the URL resolution of main in packageURL.
- Otherwise,
- Return the URL resolution of packageSubpath in packageURL.
- Throw a Module Not Found error.
PACKAGE_SELF_RESOLVE(packageName, packageSubpath, parentURL)
- Let packageURL be the result of LOOKUP_PACKAGE_SCOPE(parentURL).
- If packageURL is null, then
- Return undefined.
- Let