Node.js v13.2.1-nightly20191201cbd50262c0 Documentation


ECMAScript Modules#

Stability: 1 - Experimental

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.

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.

Enabling#

Experimental support for ECMAScript modules is enabled by default. 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" field#

Files 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 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

Package authors should include the "type" field, even in packages where all sources are CommonJS. Being explicit about the type of the package will future-proof the package in case the default type of Node.js ever changes, and it will also make things easier for build tools and loaders to determine how the files in the package should be interpreted.

Package Scope and File Extensions#

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 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 flag#

Strings 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 --input-type=module --eval "import { sep } from 'path'; console.log(sep);"

echo "import { sep } from 'path'; console.log(sep);" | node --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.

Packages#

Package Entry Points#

There are two fields that can define entry points for a package: "main" and "exports". The "main" field is supported in all versions of Node.js, but its capabilities are limited: it only defines the main entry point of the package. The "exports" field, part of Package Exports, provides an alternative to "main" where the package main entry point can be defined while also encapsulating the package, preventing any other entry points besides those defined in "exports". If package entry points are defined in both "main" and "exports", the latter takes precedence in versions of Node.js that support "exports". Conditional Exports can also be used within "exports" to define different package entry points per environment.

package.json "main"#

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 Exports#

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 ERR_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 forwards-compatible with possible 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.

Defining a "." export will define the main entry point for the package, and will always take precedence over the "main" field in the package.json.

This allows defining a different entry point for Node.js versions that support ECMAScript modules and versions that don't, for example:

{
  "main": "./main-legacy.cjs",
  "exports": {
    ".": "./main-modern.cjs"
  }
}

Conditional Exports#

Conditional exports provide a way to map to different paths depending on certain conditions. They are supported for both CommonJS and ES module imports.

For example, a package that wants to provide different ES module exports for Node.js and the browser can be written:

// ./node_modules/pkg/package.json
{
  "type": "module",
  "main": "./index.js",
  "exports": {
    "./feature": {
      "browser": "./feature-browser.js",
      "default": "./feature-default.js"
    }
  }
}

When resolving the "." export, if no matching target is found, the "main" will be used as the final fallback.

The conditions supported in Node.js are matched in the following order:

  1. "require" - matched when the package is loaded via require(). This is currently only supported behind the --experimental-conditional-exports flag.
  2. "node" - matched for any Node.js environment. Can be a CommonJS or ES module file. This is currently only supported behind the --experimental-conditional-exports flag.
  3. "default" - the generic fallback that will always match if no other more specific condition is matched first. Can be a CommonJS or ES module file.

Using the "require" condition it is possible to define a package that will have a different exported value for CommonJS and ES modules, which can be a hazard in that it can result in having two separate instances of the same package in use in an application, which can cause a number of bugs.

Other conditions such as "browser", "electron", "deno", "react-native", etc. could be defined in other runtimes or tools.

Exports Sugar#

If the "." export is the only export, the "exports" field provides sugar for this case being the direct "exports" field value.

If the "." export has a fallback array or string value, then the "exports" field can be set to this value directly.

{
  "exports": {
    ".": "./main.js"
  }
}

can be written:

{
  "exports": "./main.js"
}

When using Conditional Exports, the rule is that all keys in the object mapping must not start with a "." otherwise they would be indistinguishable from exports subpaths.

{
  "exports": {
    ".": {
      "require": "./main.cjs",
      "default": "./main.js"
    }
  }
}

can be written:

{
  "exports": {
    "require": "./main.cjs",
    "default": "./main.js"
  }
}

If writing any exports value that mixes up these two forms, an error will be thrown:

{
  // Throws on resolution!
  "exports": {
    "./feature": "./lib/feature.js",
    "require": "./main.cjs",
    "default": "./main.js"
  }
}

Dual CommonJS/ES Module Packages#

Prior to the introduction of support for ES modules in Node.js, it was a common pattern for package authors to include both CommonJS and ES module JavaScript sources in their package, with package.json "main" specifying the CommonJS entry point and package.json "module" specifying the ES module entry point. This enabled Node.js to run the CommonJS entry point while build tools such as bundlers used the ES module entry point, since Node.js ignored (and still ignores) the top-level "module" field.

Node.js can now run ES module entry points, and a package can contain both CommonJS and ES module entry points (either via separate specifiers such as 'pkg' and 'pkg/es-module', or both at the same specifier via Conditional Exports with the --experimental-conditional-exports flag). Unlike in the scenario where "module" is only used by bundlers, or ES module files are transpiled into CommonJS on the fly before evaluation by Node.js, the files referenced by the ES module entry point are evaluated as ES modules.

Dual Package Hazard#

When an application is using a package that provides both CommonJS and ES module sources, there is a risk of certain bugs if both versions of the package get loaded. This potential comes from the fact that the pkgInstance created by const pkgInstance = require('pkg') is not the same as the pkgInstance created by import pkgInstance from 'pkg' (or an alternative main path like 'pkg/module'). This is the “dual package hazard,” where two versions of the same package can be loaded within the same runtime environment. While it is unlikely that an application or package would intentionally load both versions directly, it is common for an application to load one version while a dependency of the application loads the other version. This hazard can happen because Node.js supports intermixing CommonJS and ES modules, and can lead to unexpected behavior.

If the package main export is a constructor, an instanceof comparison of instances created by the two versions returns false, and if the export is an object, properties added to one (like pkgInstance.foo = 3) are not present on the other. This differs from how import and require statements work in all-CommonJS or all-ES module environments, respectively, and therefore is surprising to users. It also differs from the behavior users are familiar with when using transpilation via tools like Babel or esm.

Writing Dual Packages While Avoiding or Minimizing Hazards#

First, the hazard described in the previous section occurs when a package contains both CommonJS and ES module sources and both sources are provided for use in Node.js, either via separate main entry points or exported paths. A package could instead be written where any version of Node.js receives only CommonJS sources, and any separate ES module sources the package may contain could be intended only for other environments such as browsers. Such a package would be usable by any version of Node.js, since import can refer to CommonJS files; but it would not provide any of the advantages of using ES module syntax.

A package could also switch from CommonJS to ES module syntax in a breaking change version bump. This has the obvious disadvantage that the newest version of the package would only be usable in ES module-supporting versions of Node.js.

Every pattern has tradeoffs, but there are two broad approaches that satisfy the following conditions:

  1. The package is usable via both require and import.
  2. The package is usable in both current Node.js and older versions of Node.js that lack support for ES modules.
  3. The package main entry point, e.g. 'pkg' can be used by both require to resolve to a CommonJS file and by import to resolve to an ES module file. (And likewise for exported paths, e.g. 'pkg/feature'.)
  4. The package provides named exports, e.g. import { name } from 'pkg' rather than import pkg from 'pkg'; pkg.name.
  5. The package is potentially usable in other ES module environments such as browsers.
  6. The hazards described in the previous section are avoided or minimized.
Approach #1: Use an ES Module Wrapper#

Write the package in CommonJS or transpile ES module sources into CommonJS, and create an ES module wrapper file that defines the named exports. Using Conditional Exports via the --experimental-conditional-exports flag, the ES module wrapper is used for import and the CommonJS entry point for require.

Note: While --experimental-conditional-exports is flagged, a package using this pattern will throw when loaded via require() in modern Node.js, unless package consumers use the --experimental-conditional-exports flag.

// ./node_modules/pkg/package.json
{
  "type": "module",
  "main": "./index.cjs",
  "exports": {
    "require": "./index.cjs",
    "default": "./wrapper.mjs"
  }
}
// ./node_modules/pkg/index.cjs
exports.name = 'value';
// ./node_modules/pkg/wrapper.mjs
import cjsModule from './index.cjs';
export const name = cjsModule.name;

In this example, the name from import { name } from 'pkg' is the same singleton as the name from const { name } = require('pkg'). Therefore === returns true when comparing the two names and the divergent specifier hazard is avoided.

If the module is not simply a list of named exports, but rather contains a unique function or object export like module.exports = function () { ... }, or if support in the wrapper for the import pkg from 'pkg' pattern is desired, then the wrapper would instead be written to export the default optionally along with any named exports as well:

import cjsModule from './index.cjs';
export const name = cjsModule.name;
export default cjsModule;

This approach is appropriate for any of the following use cases:

  • The package is currently written in CommonJS and the author would prefer not to refactor it into ES module syntax, but wishes to provide named exports for ES module consumers.
  • The package has other packages that depend on it, and the end user might install both this package and those other packages. For example a utilities package is used directly in an application, and a utilities-plus package adds a few more functions to utilities. Because the wrapper exports underlying CommonJS files, it doesn’t matter if utilities-plus is written in CommonJS or ES module syntax; it will work either way.
  • The package stores internal state, and the package author would prefer not to refactor the package to isolate its state management. See the next section.

A variant of this approach not requiring --experimental-conditional-exports for consumers could be to add an export, e.g. "./module", to point to an all-ES module-syntax version of the package. This could be used via import 'pkg/module' by users who are certain that the CommonJS version will not be loaded anywhere in the application, such as by dependencies; or if the CommonJS version can be loaded but doesn’t affect the ES module version (for example, because the package is stateless):

// ./node_modules/pkg/package.json
{
  "type": "module",
  "main": "./index.cjs",
  "exports": {
    ".": "./index.cjs",
    "./module": "./wrapper.mjs"
  }
}

If the --experimental-conditional-exports flag is dropped and therefore Conditional Exports become available without a flag, this variant could be easily updated to use conditional exports by adding conditions to the "." path; while keeping "./module" for backward compatibility.

Approach #2: Isolate State#

The most straightforward package.json would be one that defines the separate CommonJS and ES module entry points directly (requires --experimental-conditional-exports):

// ./node_modules/pkg/package.json
{
  "type": "module",
  "main": "./index.cjs",
  "exports": {
    "require": "./index.cjs",
    "default": "./index.mjs"
  }
}

This can be done if both the CommonJS and ES module versions of the package are equivalent, for example because one is the transpiled output of the other; and the package’s management of state is carefully isolated (or the package is stateless).

The reason that state is an issue is because both the CommonJS and ES module versions of the package may get used within an application; for example, the user’s application code could import the ES module version while a dependency requires the CommonJS version. If that were to occur, two copies of the package would be loaded in memory and therefore two separate states would be present. This would likely cause hard-to-troubleshoot bugs.

Aside from writing a stateless package (if JavaScript’s Math were a package, for example, it would be stateless as all of its methods are static), there are some ways to isolate state so that it’s shared between the potentially loaded CommonJS and ES module instances of the package:

  1. If possible, contain all state within an instantiated object. JavaScript’s Date, for example, needs to be instantiated to contain state; if it were a package, it would be used like this:

    import Date from 'date';
    const someDate = new Date();
    // someDate contains state; Date does not
    

    The new keyword isn’t required; a package’s function can return a new object, or modify a passed-in object, to keep the state external to the package.

  2. Isolate the state in one or more CommonJS files that are shared between the CommonJS and ES module versions of the package. For example, if the CommonJS and ES module entry points are index.cjs and index.mjs, respectively:

    // ./node_modules/pkg/index.cjs
    const state = require('./state.cjs');
    module.exports.state = state;
    
    // ./node_modules/pkg/index.mjs
    import state from './state.cjs';
    export {
      state
    };
    

    Even if pkg is used via both require and import in an application (for example, via import in application code and via require by a dependency) each reference of pkg will contain the same state; and modifying that state from either module system will apply to both.

Any plugins that attach to the package’s singleton would need to separately attach to both the CommonJS and ES module singletons.

This approach is appropriate for any of the following use cases:

  • The package is currently written in ES module syntax and the package author wants that version to be used wherever such syntax is supported.
  • The package is stateless or its state can be isolated without too much difficulty.
  • The package is unlikely to have other public packages that depend on it, or if it does, the package is stateless or has state that need not be shared between dependencies or with the overall application.

Even with isolated state, there is still the cost of possible extra code execution between the CommonJS and ES module versions of a package.

As with the previous approach, a variant of this approach not requiring --experimental-conditional-exports for consumers could be to add an export, e.g. "./module", to point to an all-ES module-syntax version of the package:

// ./node_modules/pkg/package.json
{
  "type": "module",
  "main": "./index.cjs",
  "exports": {
    ".": "./index.cjs",
    "./module": "./index.mjs"
  }
}

If the --experimental-conditional-exports flag is dropped and therefore Conditional Exports become available without a flag, this variant could be easily updated to use conditional exports by adding conditions to the "." path; while keeping "./module" for backward compatibility.

import Specifiers#

Terminology#

The 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: and data: 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:///.

data: Imports#

data: URLs are supported for importing with the following MIME types:

  • text/javascript for ES Modules
  • application/json for JSON
  • application/wasm for WASM.

data: URLs only resolve Bare specifiers for builtin modules and Absolute specifiers. Resolving Relative specifiers will not work because data: is not a special scheme. For example, attempting to load ./foo from data:text/javascript,import "./foo"; will fail to resolve since there is no concept of relative resolution for data: URLs. An example of a data: URLs being used is:

import 'data:text/javascript,console.log("hello!");';
import _ from 'data:application/json,"world!"';

import.meta#

The import.meta metaproperty is an Object that contains the following property:

  • url <string> The absolute file: URL of the module.

Differences Between ES Modules and CommonJS#

Mandatory file extensions#

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.

No NODE_PATH#

NODE_PATH is not part of resolving import specifiers. Please use symlinks if this behavior is desired.

No 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);

No require.extensions#

require.extensions is not used by import. The expectation is that loader hooks can provide this workflow in the future.

No require.cache#

require.cache is not used by import. It has a separate cache.

URL-based paths#

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.

Interoperability with CommonJS#

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 statements#

An import statement can reference an ES module or a CommonJS module. Other file types such as JSON or 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; unless the package’s package.json contains an "exports" field, in which case files within packages need to be accessed via the path defined in "exports".

import { sin, cos } from 'geometry/trigonometry-functions.mjs';

Only the “default export” is supported for CommonJS files or packages:

import packageMain from 'commonjs-package'; // Works

import { method } from 'commonjs-package'; // Errors

import() expressions#

Dynamic 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#

CommonJS, JSON, and Native modules can be used with module.createRequire().

// cjs.cjs
module.exports = 'cjs';

// esm.mjs
import { createRequire } from 'module';

const require = createRequire(import.meta.url);

const cjs = require('./cjs.cjs');
cjs === 'cjs'; // true

Builtin modules#

Builtin modules will 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 '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';
import { syncBuiltinESMExports } from 'module';

fs.readFileSync = () => Buffer.from('Hello, ESM');
syncBuiltinESMExports();

fs.readFileSync === readFileSync;

Experimental JSON Modules#

Currently importing JSON modules are only supported in the commonjs mode and are loaded using the CJS loader. WHATWG JSON modules specification are still being standardized, and are experimentally supported by including the additional flag --experimental-json-modules when running Node.js.

When the --experimental-json-modules flag is included both the commonjs and module mode will use the new experimental JSON loader. 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';

The --experimental-json-modules flag is needed for the module to work.

node index.mjs # fails
node --experimental-json-modules index.mjs # works

Experimental Wasm Modules#

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-wasm-modules index.mjs

would provide the exports interface for the instantiation of module.wasm.

Experimental Loader hooks#

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 --experimental-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.

Resolve hook#

The resolve hook returns the resolved file URL and module format for a given module specifier and parent file URL:

import { URL, pathToFileURL } from 'url';
const baseURL = pathToFileURL(process.cwd()).href;

/**
 * @param {string} specifier
 * @param {string} parentModuleURL
 * @param {function} defaultResolver
 */
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:

formatDescription
'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';
import { URL, pathToFileURL } from 'url';

const builtins = Module.builtinModules;
const JS_EXTENSIONS = new Set(['.js', '.mjs']);

const baseURL = pathToFileURL(process.cwd()).href;

/**
 * @param {string} specifier
 * @param {string} parentModuleURL
 * @param {function} defaultResolver
 */
export async function resolve(specifier,
                              parentModuleURL = baseURL,
                              defaultResolver) {
  if (builtins.includes(specifier)) {
    return {
      url: specifier,
      format: 'builtin'
    };
  }
  if (/^\.{0,2}[/]/.test(specifier) !== true && !specifier.startsWith('file:')) {
    // For node_modules support:
    // return defaultResolver(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-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).

Dynamic instantiate hook#

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.

Resolution Algorithm#

Features#

The resolver has the following properties:

  • FileURL-based resolution as is used by ES modules
  • Support for builtin module loading
  • Relative and absolute URL resolution
  • No default extensions
  • No folder mains
  • Bare specifier package resolution lookup through node_modules

Resolver 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, 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.

defaultEnv is the conditional environment name priority array, ["node", "default"].

Resolver algorithm specification

ESM_RESOLVE(specifier, parentURL, isMain)

  1. Let resolvedURL be undefined.
  2. If specifier is a valid URL, then

    1. Set resolvedURL to the result of parsing and reserializing specifier as a URL.
  3. Otherwise, if specifier starts with "/", then

    1. Throw an Invalid Specifier error.
  4. Otherwise, if specifier starts with "./" or "../", then

    1. Set resolvedURL to the URL resolution of specifier relative to parentURL.
  5. Otherwise,

    1. Note: specifier is now a bare specifier.
    2. Set resolvedURL the result of PACKAGE_RESOLVE(specifier, parentURL).
  6. If resolvedURL contains any percent encodings of "/" or "\" ("%2f" and "%5C" respectively), then

    1. Throw an Invalid Specifier error.
  7. If the file at resolvedURL does not exist, then

    1. Throw a Module Not Found error.
  8. Set resolvedURL to the real path of resolvedURL.
  9. Let format be the result of ESM_FORMAT(resolvedURL, isMain).
  10. Load resolvedURL as module format, format.

PACKAGE_RESOLVE(packageSpecifier, parentURL)

  1. Let packageName be undefined.
  2. Let packageSubpath be undefined.
  3. If packageSpecifier is an empty string, then

    1. Throw an Invalid Specifier error.
  4. Otherwise,

    1. If packageSpecifier does not contain a "/" separator, then

      1. Throw an Invalid Specifier error.
    2. Set packageName to the substring of packageSpecifier until the second "/" separator or the end of the string.
  5. If packageName starts with "." or contains "\" or "%", then

    1. Throw an Invalid Specifier error.
  6. Let packageSubpath be undefined.
  7. If the length of packageSpecifier is greater than the length of packageName, then

    1. Set packageSubpath to "." concatenated with the substring of packageSpecifier from the position at the length of packageName.
  8. If packageSubpath contains any "." or ".." segments or percent encoded strings for "/" or "\", then

    1. Throw an Invalid Specifier error.
  9. If packageSubpath is undefined and packageName is a Node.js builtin module, then

    1. Return the string "node:" concatenated with packageSpecifier.
  10. While parentURL is not the file system root,

    1. Let packageURL be the URL resolution of "node_modules/" concatenated with packageSpecifier, relative to parentURL.
    2. Set parentURL to the parent folder URL of parentURL.
    3. If the folder at packageURL does not exist, then

      1. Set parentURL to the parent URL path of parentURL.
      2. Continue the next loop iteration.
    4. Let pjson be the result of READ_PACKAGE_JSON(packageURL).
    5. If packageSubpath is _undefined__, then

      1. Return the result of PACKAGE_MAIN_RESOLVE(packageURL, pjson).
    6. Otherwise,

      1. If pjson is not null and pjson has an "exports" key, then

        1. Let exports be pjson.exports.
        2. If exports is not null or undefined, then

          1. Return PACKAGE_EXPORTS_RESOLVE(packageURL, packageSubpath, pjson.exports).
      2. Return the URL resolution of packageSubpath in packageURL.
  11. Set selfUrl to the result of SELF_REFERENCE_RESOLE(packageSpecifier, parentURL).
  12. If selfUrl isn't empty, return selfUrl.
  13. Throw a Module Not Found error.

SELF_REFERENCE_RESOLVE(specifier, parentURL)

  1. Let packageURL be the result of READ_PACKAGE_SCOPE(parentURL).
  2. If packageURL is null, then

    1. Return an empty result.
  3. Let pjson be the result of READ_PACKAGE_JSON(packageURL).
  4. Set name to pjson.name.
  5. If name is empty, then return an empty result.
  6. If name is equal to specifier, then

    1. Return the result of PACKAGE_MAIN_RESOLVE(packageURL, pjson).
  7. If specifier starts with name followed by "/", then

    1. Set subpath to everything after the "/".
    2. If pjson is not null and pjson has an "exports" key, then

      1. Let exports be pjson.exports.
      2. If exports is not null or undefined, then

        1. Return PACKAGE_EXPORTS_RESOLVE(packageURL, subpath, pjson.exports).
    3. Return the URL resolution of subpath in packageURL.
  8. Otherwise return an empty result.

PACKAGE_MAIN_RESOLVE(packageURL, pjson)

  1. If pjson is null, then

    1. Throw a Module Not Found error.
  2. If pjson.exports is not null or undefined, then

    1. If exports is an Object with both a key starting with "." and a key not starting with ".", throw an "Invalid Package Configuration" error.
    2. If pjson.exports is a String or Array, or an Object containing no keys starting with ".", then

      1. Return PACKAGE_EXPORTS_TARGET_RESOLVE(packageURL, pjson.exports, "").
    3. If pjson.exports is an Object containing a "." property, then

      1. Let mainExport be the "." property in pjson.exports.
      2. Return PACKAGE_EXPORTS_TARGET_RESOLVE(packageURL, mainExport, "").
  3. If pjson.main is a String, then

    1. Let resolvedMain be the URL resolution of packageURL, "/", and pjson.main.
    2. If the file at resolvedMain exists, then

      1. Return resolvedMain.
  4. If pjson.type is equal to "module", then

    1. Throw a Module Not Found error.
  5. Let legacyMainURL be the result applying the legacy LOAD_AS_DIRECTORY CommonJS resolver to packageURL, throwing a Module Not Found error for no resolution.
  6. Return legacyMainURL.

PACKAGE_EXPORTS_RESOLVE(packageURL, packagePath, exports)

  1. If exports is an Object with both a key starting with "." and a key not starting with ".", throw an "Invalid Package Configuration" error.
  2. If exports is an Object and all keys of exports start with ".", then

    1. Set packagePath to "./" concatenated with packagePath.
    2. If packagePath is a key of exports, then

      1. Let target be the value of exports[packagePath].
      2. Return PACKAGE_EXPORTS_TARGET_RESOLVE(packageURL, target, "", defaultEnv).
    3. Let directoryKeys be the list of keys of exports ending in "/", sorted by length descending.
    4. For each key directory in directoryKeys, do

      1. If packagePath starts with directory, then

        1. Let target be the value of exports[directory].
        2. Let subpath be the substring of target starting at the index of the length of directory.
        3. Return PACKAGE_EXPORTS_TARGET_RESOLVE(packageURL, target, subpath, defaultEnv).
  3. Throw a Module Not Found error.

PACKAGE_EXPORTS_TARGET_RESOLVE(packageURL, target, subpath, env)

  1. If target is a String, then

    1. If target does not start with "./", throw a Module Not Found error.
    2. If subpath has non-zero length and target does not end with "/", throw a Module Not Found error.
    3. If target or subpath contain any "node_modules" segments including "node_modules" percent-encoding, throw a Module Not Found error.
    4. Let resolvedTarget be the URL resolution of the concatenation of packageURL and target.
    5. If resolvedTarget is contained in packageURL, then

      1. Let resolved be the URL resolution of the concatenation of subpath and resolvedTarget.
      2. If resolved is contained in resolvedTarget, then

        1. Return resolved.
  2. Otherwise, if target is a non-null Object, then

    1. If target has an object key matching one of the names in env, then

      1. Let targetValue be the corresponding value of the first object key of target in env.
      2. Let resolved be the result of PACKAGE_EXPORTS_TARGET_RESOLVE (packageURL, targetValue, subpath, env).
      3. Assert: resolved is a String.
      4. Return resolved.
  3. Otherwise, if target is an Array, then

    1. For each item targetValue in target, do

      1. If targetValue is an Array, continue the loop.
      2. Let resolved be the result of PACKAGE_EXPORTS_TARGET_RESOLVE(packageURL, targetValue, subpath, env), continuing the loop on abrupt completion.
      3. Assert: resolved is a String.
      4. Return resolved.
  4. Throw a Module Not Found error.

ESM_FORMAT(url, isMain)

  1. Assert: url corresponds to an existing file.
  2. Let pjson be the result of READ_PACKAGE_SCOPE(url).
  3. If url ends in ".mjs", then

    1. Return "module".
  4. If url ends in ".cjs", then

    1. Return "commonjs".
  5. If pjson?.type exists and is "module", then

    1. If isMain is true or url ends in ".js", then

      1. Return "module".
    2. Throw an Unsupported File Extension error.
  6. Otherwise,

    1. If isMain is true, then

      1. Return "commonjs".
    2. Throw an Unsupported File Extension error.

READ_PACKAGE_SCOPE(url)

  1. Let scopeURL be url.
  2. While scopeURL is not the file system root,

    1. If scopeURL ends in a "node_modules" path segment, return null.
    2. Let pjson be the result of READ_PACKAGE_JSON(scopeURL).
    3. If pjson is not null, then

      1. Return pjson.
    4. Set scopeURL to the parent URL of scopeURL.
  3. Return null.

READ_PACKAGE_JSON(packageURL)

  1. Let pjsonURL be the resolution of "package.json" within packageURL.
  2. If the file at pjsonURL does not exist, then

    1. Return null.
  3. If the file at packageURL does not parse as valid JSON, then

    1. Throw an Invalid Package Configuration error.
  4. Return the parsed JSON source of the file at pjsonURL.

Customizing ESM specifier resolution algorithm#

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 index.mjs
success!
$ node index # Failure!
Error: Cannot find module
$ node --es-module-specifier-resolution=node index
success!