diff --git a/.eslintrc.js b/.eslintrc.js
index 54595a8b74d9a9..d3a6c73241f1ff 100644
--- a/.eslintrc.js
+++ b/.eslintrc.js
@@ -38,6 +38,8 @@ module.exports = {
{
files: [
'doc/api/esm.md',
+ 'test/es-module/test-esm-type-flag.js',
+ 'test/es-module/test-esm-type-flag-alias.js',
'*.mjs',
'test/es-module/test-esm-example-loader.js',
],
diff --git a/doc/api/cli.md b/doc/api/cli.md
index 4c5c8fb61853cf..ad7b65e90a827e 100644
--- a/doc/api/cli.md
+++ b/doc/api/cli.md
@@ -131,9 +131,43 @@ conjunction with native stack and other runtime environment data.
added: v6.0.0
-->
+### `--entry-type=type`
+
+
+Used with `--experimental-modules`, this configures Node.js to interpret the
+initial entry point as CommonJS or as an ES module.
+
+Valid values are `"commonjs"` and `"module"`. The default is to infer from
+the file extension and the `"type"` field in the nearest parent `package.json`.
+
+Works for executing a file as well as `--eval`, `--print`, `STDIN`.
+
Enable FIPS-compliant crypto at startup. (Requires Node.js to be built with
`./configure --openssl-fips`.)
+### `--es-module-specifier-resolution=mode`
+
+
+To be used in conjunction with `--experimental-modules`. Sets the resolution
+algorithm for resolving specifiers. Valid options are `explicit` and `node`.
+
+The default is `explicit`, which requires providing the full path to a
+module. The `node` mode will enable support for optional file extensions and
+the ability to import a directory that has an index file.
+
+Please see [customizing esm specifier resolution][] for example usage.
+
+### `--experimental-json-modules`
+
+
+Enable experimental JSON support for the ES Module loader.
+
### `--experimental-modules`
+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][].
+[Node.js EP for ES Modules][] and the [ECMAScript-modules implementation][].
-Not all features of the EP are complete and will be landing as both VM support
-and implementation is ready. Error messages are still being polished.
+Expect major changes in the implementation including interoperability support,
+specifier resolution, and default behavior.
## Enabling
-The `--experimental-modules` flag can be used to enable features for loading
-ESM modules.
+The `--experimental-modules` flag can be used to enable support for
+ECMAScript modules (ES modules).
+
+## Running Node.js with an ECMAScript Module
-Once this has been set, files ending with `.mjs` will be able to be loaded
-as ES Modules.
+There are a few ways to start Node.js with an ES module as its input.
+
+### Initial entry point with an .mjs extension
+
+A file ending with `.mjs` passed to Node.js as an initial entry point will be
+loaded as an ES module.
```sh
node --experimental-modules my-app.mjs
```
-## Features
+### --entry-type=module flag
-
+Files ending with `.js` or `.mjs`, or lacking any extension,
+will be loaded as ES modules when the `--entry-type=module` flag is set.
+
+```sh
+node --experimental-modules --entry-type=module my-app.js
+```
+
+For completeness there is also `--entry-type=commonjs`, for explicitly running
+a `.js` file as CommonJS. This is the default behavior if `--entry-type` is
+unspecified.
+
+The `--entry-type=module` flag can also be used to configure Node.js to treat
+as an ES module input sent in via `--eval` or `--print` (or `-e` or `-p`) or
+piped to Node.js via `STDIN`.
+
+```sh
+node --experimental-modules --entry-type=module --eval \
+ "import { sep } from 'path'; console.log(sep);"
+
+echo "import { sep } from 'path'; console.log(sep);" | \
+ node --experimental-modules --entry-type=module
+```
+
+### 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.
+
+
+```js
+// package.json
+{
+ "type": "module"
+}
+```
+
+```sh
+# 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.
+
+## 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
+--experimental-modules my-app.js`) but also to files referenced by `import`
+statements and `import()` expressions.
+
+```js
+// 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.
+
+```js
+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).
+
+## Package Entry Points
+
+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`.
+
+
+```js
+// ./node_modules/es-module-package/package.json
+{
+ "type": "module",
+ "main": "./src/index.js"
+}
+```
+```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.
+
+> Currently a package can define _either_ a CommonJS entry point **or** an ES
+> module entry point; there is no way to specify separate entry points for
+> CommonJS and ES module usage. This means that a package entry point can be
+> included via `require` or via `import` but not both.
+>
+> Such a limitation makes it difficult for packages to support both new versions
+> of Node.js that understand ES modules and older versions of Node.js that
+> understand only CommonJS. There is work ongoing to remove this limitation, and
+> it will very likely entail changes to the behavior of `"main"` as defined
+> here.
+
+## 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.
-### Supported
+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.
-Only the CLI argument for the main entry point to the program can be an entry
-point into an ESM graph. Dynamic import can also be used to create entry points
-into ESM graphs at runtime.
+Specifiers may not begin with `/` or `//`. These are reserved for potential
+future use. The root of the current volume may be referenced via `file:///`.
-#### import.meta
+## import.meta
* {Object}
@@ -46,63 +240,128 @@ property:
* `url` {string} The absolute `file:` URL of the module.
-### Unsupported
+## Differences Between ES Modules and CommonJS
-| Feature | Reason |
-| --- | --- |
-| `require('./foo.mjs')` | ES Modules have differing resolution and timing, use dynamic import |
+### 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.
-## Notable differences between `import` and `require`
+This behavior matches how `import` behaves in browser environments, assuming a
+typically configured server.
-### No NODE_PATH
+### No NODE_PATH
`NODE_PATH` is not part of resolving `import` specifiers. Please use symlinks
if this behavior is desired.
-### No `require.extensions`
+### 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.createRequireFromPath()`][].
+
+An equivalent for `__filename` and `__dirname` is [`import.meta.url`][].
+
+### 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`
+### No require.cache
`require.cache` is not used by `import`. It has a separate cache.
-### URL based paths
+### URL-based paths
-ESM are resolved and cached based upon [URL](https://url.spec.whatwg.org/)
-semantics. This means that files containing special characters such as `#` and
-`?` need to be escaped.
+ES modules are resolved and cached based upon
+[URL](https://url.spec.whatwg.org/) 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.
```js
-import './foo?query=1'; // loads ./foo with query of "?query=1"
-import './foo?query=2'; // loads ./foo with query of "?query=2"
+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.
-## Interop with existing modules
+## Interoperability with CommonJS
+
+### require
-All CommonJS, JSON, and C++ modules can be used with `import`.
+`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.createRequireFromPath()`][].
-Modules loaded this way will only be loaded once, even if their query
-or fragment string differs between `import` statements.
+To include an ES module into CommonJS, use [`import()`][].
-When loaded via `import` these modules will provide a single `default` export
-representing the value of `module.exports` at the time they finished evaluating.
+### import statements
+
+An `import` statement can reference either ES module or CommonJS JavaScript.
+Other file types such as JSON and Native modules are not supported. For those,
+use [`module.createRequireFromPath()`][].
+
+`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.
```js
-// foo.js
-module.exports = { one: 1 };
+import { sin, cos } from 'geometry/trigonometry-functions.mjs';
+```
+
+> Currently only the “default export” is supported for CommonJS files or
+> packages:
+>
+>
+> ```js
+> import packageMain from 'commonjs-package'; // Works
+>
+> import { method } from 'commonjs-package'; // Errors
+> ```
+>
+> There are ongoing efforts to make the latter code possible.
-// bar.mjs
-import foo from './foo.js';
-foo.one === 1; // true
+### import() expressions
+
+Dynamic `import()` is supported in both CommonJS and ES modules. It can be used
+to include ES module files from CommonJS code.
+
+```js
+(async () => {
+ await import('./my-app.mjs');
+})();
```
+## CommonJS, JSON, and Native Modules
+
+CommonJS, JSON, and Native modules can be used with [`module.createRequireFromPath()`][].
+
+```js
+// cjs.js
+module.exports = 'cjs';
+
+// esm.mjs
+import { createRequireFromPath as 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
+
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
@@ -132,7 +391,41 @@ fs.readFileSync = () => Buffer.from('Hello, ESM');
fs.readFileSync === readFileSync;
```
-## Loader hooks
+## Experimental JSON Modules
+
+**Note: This API is still being designed and is subject to change.**
+
+Currently importing JSON modules are only supported in the `commonjs` mode
+and are loaded using the CJS loader. [WHATWG JSON modules][] are currently
+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.js` with
+
+
+```js
+import packageConfig from './package.json';
+```
+
+The `--experimental-json-modules` flag is needed for the module
+to work.
+
+```bash
+node --experimental-modules --entry-type=module index.js # fails
+node --experimental-modules --entry-type=module --experimental-json-modules index.js # works
+```
+
+## Experimental Loader hooks
+
+**Note: This API is currently being redesigned and will still change.**
@@ -173,11 +466,10 @@ module. This can be one of the following:
| `format` | Description |
| --- | --- |
-| `'esm'` | Load a standard JavaScript module |
-| `'cjs'` | Load a node-style CommonJS module |
-| `'builtin'` | Load a node builtin CommonJS module |
+| `'module'` | Load a standard JavaScript module |
+| `'commonjs'` | Load a Node.js CommonJS module |
+| `'builtin'` | Load a Node.js builtin module |
| `'json'` | Load a JSON file |
-| `'addon'` | Load a [C++ Addon][addons] |
| `'dynamic'` | Use a [dynamic instantiate hook][] |
For example, a dummy loader to load JavaScript restricted to browser resolution
@@ -253,6 +545,184 @@ 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 _"wasm"_ or _"addon"_ can be
+extended in future updates.
+
+In the following algorithms, all subroutine errors are propagated as errors
+of these top-level routines.
+
+_isMain_ is **true** when resolving the Node.js application entry point.
+
+When using the `--entry-type` flag, it overrides the ESM_FORMAT result while
+providing errors in the case of explicit conflicts.
+
+
+Resolver algorithm specification
+
+**ESM_RESOLVE(_specifier_, _parentURL_, _isMain_)**
+> 1. Let _resolvedURL_ be **undefined**.
+> 1. If _specifier_ is a valid URL, then
+> 1. Set _resolvedURL_ to the result of parsing and reserializing
+> _specifier_ as a URL.
+> 1. Otherwise, if _specifier_ starts with _"/"_, then
+> 1. Throw an _Invalid Specifier_ error.
+> 1. Otherwise, if _specifier_ starts with _"./"_ or _"../"_, then
+> 1. Set _resolvedURL_ to the URL resolution of _specifier_ relative to
+> _parentURL_.
+> 1. Otherwise,
+> 1. Note: _specifier_ is now a bare specifier.
+> 1. Set _resolvedURL_ the result of
+> **PACKAGE_RESOLVE**(_specifier_, _parentURL_).
+> 1. If the file at _resolvedURL_ does not exist, then
+> 1. Throw a _Module Not Found_ error.
+> 1. Set _resolvedURL_ to the real path of _resolvedURL_.
+> 1. Let _format_ be the result of **ESM_FORMAT**(_resolvedURL_, _isMain_).
+> 1. Load _resolvedURL_ as module format, _format_.
+
+PACKAGE_RESOLVE(_packageSpecifier_, _parentURL_)
+> 1. Let _packageName_ be *undefined*.
+> 1. Let _packageSubpath_ be *undefined*.
+> 1. If _packageSpecifier_ is an empty string, then
+> 1. Throw an _Invalid Specifier_ error.
+> 1. If _packageSpecifier_ does not start with _"@"_, then
+> 1. Set _packageName_ to the substring of _packageSpecifier_ until the
+> first _"/"_ separator or the end of the string.
+> 1. Otherwise,
+> 1. If _packageSpecifier_ does not contain a _"/"_ separator, then
+> 1. Throw an _Invalid Specifier_ error.
+> 1. Set _packageName_ to the substring of _packageSpecifier_
+> until the second _"/"_ separator or the end of the string.
+> 1. Let _packageSubpath_ be the substring of _packageSpecifier_ from the
+> position at the length of _packageName_ plus one, if any.
+> 1. Assert: _packageName_ is a valid package name or scoped package name.
+> 1. Assert: _packageSubpath_ is either empty, or a path without a leading
+> separator.
+> 1. If _packageSubpath_ contains any _"."_ or _".."_ segments or percent
+> encoded strings for _"/"_ or _"\\"_ then,
+> 1. Throw an _Invalid Specifier_ error.
+> 1. If _packageSubpath_ is empty and _packageName_ is a Node.js builtin
+> module, then
+> 1. Return the string _"node:"_ concatenated with _packageSpecifier_.
+> 1. While _parentURL_ is not the file system root,
+> 1. Let _packageURL_ be the URL resolution of "node_modules/"
+> concatenated with _packageSpecifier_, relative to _parentURL_.
+> 1. Set _parentURL_ to the parent folder URL of _parentURL_.
+> 1. If the folder at _packageURL_ does not exist, then
+> 1. Set _parentURL_ to the parent URL path of _parentURL_.
+> 1. Continue the next loop iteration.
+> 1. Let _pjson_ be the result of **READ_PACKAGE_JSON**(_packageURL_).
+> 1. If _packageSubpath_ is empty, then
+> 1. Return the result of **PACKAGE_MAIN_RESOLVE**(_packageURL_,
+> _pjson_).
+> 1. Otherwise,
+> 1. Return the URL resolution of _packageSubpath_ in _packageURL_.
+> 1. Throw a _Module Not Found_ error.
+
+PACKAGE_MAIN_RESOLVE(_packageURL_, _pjson_)
+> 1. If _pjson_ is **null**, then
+> 1. Throw a _Module Not Found_ error.
+> 1. If _pjson.main_ is a String, then
+> 1. Let _resolvedMain_ be the concatenation of _packageURL_, "/", and
+> _pjson.main_.
+> 1. If the file at _resolvedMain_ exists, then
+> 1. Return _resolvedMain_.
+> 1. If _pjson.type_ is equal to _"module"_, then
+> 1. Throw a _Module Not Found_ error.
+> 1. Let _legacyMainURL_ be the result applying the legacy
+> **LOAD_AS_DIRECTORY** CommonJS resolver to _packageURL_, throwing a
+> _Module Not Found_ error for no resolution.
+> 1. If _legacyMainURL_ does not end in _".js"_ then,
+> 1. Throw an _Unsupported File Extension_ error.
+> 1. Return _legacyMainURL_.
+
+**ESM_FORMAT(_url_, _isMain_)**
+> 1. Assert: _url_ corresponds to an existing file.
+> 1. Let _pjson_ be the result of **READ_PACKAGE_SCOPE**(_url_).
+> 1. If _url_ ends in _".mjs"_, then
+> 1. Return _"module"_.
+> 1. If _url_ ends in _".cjs"_, then
+> 1. Return _"commonjs"_.
+> 1. If _pjson?.type_ exists and is _"module"_, then
+> 1. If _isMain_ is **true** or _url_ ends in _".js"_, then
+> 1. Return _"module"_.
+> 1. Throw an _Unsupported File Extension_ error.
+> 1. Otherwise,
+> 1. If _isMain_ is **true** or _url_ ends in _".js"_, _".json"_ or
+> _".node"_, then
+> 1. Return _"commonjs"_.
+> 1. Throw an _Unsupported File Extension_ error.
+
+READ_PACKAGE_SCOPE(_url_)
+> 1. Let _scopeURL_ be _url_.
+> 1. While _scopeURL_ is not the file system root,
+> 1. Let _pjson_ be the result of **READ_PACKAGE_JSON**(_scopeURL_).
+> 1. If _pjson_ is not **null**, then
+> 1. Return _pjson_.
+> 1. Set _scopeURL_ to the parent URL of _scopeURL_.
+> 1. Return **null**.
+
+READ_PACKAGE_JSON(_packageURL_)
+> 1. Let _pjsonURL_ be the resolution of _"package.json"_ within _packageURL_.
+> 1. If the file at _pjsonURL_ does not exist, then
+> 1. Return **null**.
+> 1. If the file at _packageURL_ does not parse as valid JSON, then
+> 1. Throw an _Invalid Package Configuration_ error.
+> 1. 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.
+
+```bash
+$ 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!
+```
+
+[`export`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export
+[`import`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
+[`import()`]: #esm_import-expressions
+[`import.meta.url`]: #esm_import_meta
+[`module.createRequireFromPath()`]: modules.html#modules_module_createrequirefrompath_filename
+[CommonJS]: modules.html
+[ECMAScript-modules implementation]: https://github.com/nodejs/modules/blob/master/doc/plan-for-new-modules-implementation.md
[Node.js EP for ES Modules]: https://github.com/nodejs/node-eps/blob/master/002-es-modules.md
-[addons]: addons.html
+[WHATWG JSON modules]: https://github.com/whatwg/html/issues/4315
[dynamic instantiate hook]: #esm_dynamic_instantiate_hook
+[the official standard format]: https://tc39.github.io/ecma262/#sec-modules
diff --git a/doc/node.1 b/doc/node.1
index 7bcb1edc59ab5d..e2ac998cab2973 100644
--- a/doc/node.1
+++ b/doc/node.1
@@ -119,6 +119,15 @@ Enable FIPS-compliant crypto at startup.
Requires Node.js to be built with
.Sy ./configure --openssl-fips .
.
+.It Fl -entry-type Ns = Ns Ar type
+Set the top-level module resolution type.
+.
+.It Fl -es-module-specifier-resolution
+Select extension resolution algorithm for ES Modules; either 'explicit' (default) or 'node'
+.
+.It Fl -experimental-json-modules
+Enable experimental JSON interop support for the ES Module loader.
+.
.It Fl -experimental-modules
Enable experimental ES module support and caching modules.
.
diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js
index 820d931575b1bc..20b362f2136583 100644
--- a/lib/internal/bootstrap/pre_execution.js
+++ b/lib/internal/bootstrap/pre_execution.js
@@ -323,6 +323,10 @@ function initializeESMLoader() {
const userLoader = getOptionValue('--loader');
// If --loader is specified, create a loader with user hooks. Otherwise
// create the default loader.
+ if (userLoader) {
+ const { emitExperimentalWarning } = require('internal/util');
+ emitExperimentalWarning('--loader');
+ }
esm.initializeLoader(process.cwd(), userLoader);
}
}
diff --git a/lib/internal/errors.js b/lib/internal/errors.js
index 42775691c37251..e3347374cc702b 100644
--- a/lib/internal/errors.js
+++ b/lib/internal/errors.js
@@ -678,6 +678,24 @@ E('ERR_ENCODING_INVALID_ENCODED_DATA', function(encoding, ret) {
}, TypeError);
E('ERR_ENCODING_NOT_SUPPORTED', 'The "%s" encoding is not supported',
RangeError);
+E('ERR_ENTRY_TYPE_MISMATCH', (filename, ext, typeFlag, conflict) => {
+ const typeString =
+ typeFlag === 'module' ? '--entry-type=module' : '--entry-type=commonjs';
+ // --entry-type mismatches file extension
+ if (conflict === 'extension') {
+ return `Extension ${ext} is not supported for ` +
+ `${typeString} loading ${filename}`;
+ }
+ assert(
+ conflict === 'scope',
+ '"conflict" value unknown. Set this argument to "extension" or "scope"'
+ );
+ // --entry-type mismatches package.json "type"
+ return `Cannot use ${typeString} because nearest parent package.json ` +
+ ((typeFlag === 'module') ?
+ 'includes "type": "commonjs"' : 'includes "type": "module",') +
+ ` which controls the type to use for ${filename}`;
+}, TypeError);
E('ERR_FALSY_VALUE_REJECTION', function(reason) {
this.reason = reason;
return 'Promise was rejected with falsy value';
@@ -864,6 +882,8 @@ E('ERR_INVALID_OPT_VALUE', (name, value) =>
RangeError);
E('ERR_INVALID_OPT_VALUE_ENCODING',
'The value "%s" is invalid for option "encoding"', TypeError);
+E('ERR_INVALID_PACKAGE_CONFIG',
+ 'Invalid package config in \'%s\' imported from %s', Error);
E('ERR_INVALID_PERFORMANCE_MARK',
'The "%s" performance mark has not been set', Error);
E('ERR_INVALID_PROTOCOL',
@@ -958,11 +978,6 @@ E('ERR_MISSING_ARGS',
E('ERR_MISSING_DYNAMIC_INSTANTIATE_HOOK',
'The ES Module loader may not return a format of \'dynamic\' when no ' +
'dynamicInstantiate function was provided', Error);
-E('ERR_MISSING_MODULE', 'Cannot find module %s', Error);
-E('ERR_MODULE_RESOLUTION_LEGACY',
- '%s not found by import in %s.' +
- ' Legacy behavior in require() would have found it at %s',
- Error);
E('ERR_MULTIPLE_CALLBACK', 'Callback called multiple times', Error);
E('ERR_NAPI_CONS_FUNCTION', 'Constructor must be a function', TypeError);
E('ERR_NAPI_INVALID_DATAVIEW_ARGS',
@@ -1067,9 +1082,7 @@ E('ERR_UNHANDLED_ERROR',
E('ERR_UNKNOWN_BUILTIN_MODULE', 'No such built-in module: %s', Error);
E('ERR_UNKNOWN_CREDENTIAL', '%s identifier does not exist: %s', Error);
E('ERR_UNKNOWN_ENCODING', 'Unknown encoding: %s', TypeError);
-
-// This should probably be a `TypeError`.
-E('ERR_UNKNOWN_FILE_EXTENSION', 'Unknown file extension: %s', Error);
+E('ERR_UNKNOWN_FILE_EXTENSION', 'Unknown file extension: %s', TypeError);
E('ERR_UNKNOWN_MODULE_FORMAT', 'Unknown module format: %s', RangeError);
E('ERR_UNKNOWN_SIGNAL', 'Unknown signal: %s', TypeError);
diff --git a/lib/internal/main/check_syntax.js b/lib/internal/main/check_syntax.js
index 7df70b272091ff..8c73d522edbf69 100644
--- a/lib/internal/main/check_syntax.js
+++ b/lib/internal/main/check_syntax.js
@@ -11,12 +11,18 @@ const {
readStdin
} = require('internal/process/execution');
-const CJSModule = require('internal/modules/cjs/loader');
+const { pathToFileURL } = require('url');
+
const vm = require('vm');
const {
stripShebang, stripBOM
} = require('internal/modules/cjs/helpers');
+let CJSModule;
+function CJSModuleInit() {
+ if (!CJSModule)
+ CJSModule = require('internal/modules/cjs/loader');
+}
if (process.argv[1] && process.argv[1] !== '-') {
// Expand process.argv[1] into a full path.
@@ -25,7 +31,7 @@ if (process.argv[1] && process.argv[1] !== '-') {
// TODO(joyeecheung): not every one of these are necessary
prepareMainThreadExecution();
-
+ CJSModuleInit();
// Read the source.
const filename = CJSModule._resolveFilename(process.argv[1]);
@@ -34,20 +40,40 @@ if (process.argv[1] && process.argv[1] !== '-') {
markBootstrapComplete();
- checkScriptSyntax(source, filename);
+ checkSyntax(source, filename);
} else {
// TODO(joyeecheung): not every one of these are necessary
prepareMainThreadExecution();
+ CJSModuleInit();
markBootstrapComplete();
readStdin((code) => {
- checkScriptSyntax(code, '[stdin]');
+ checkSyntax(code, '[stdin]');
});
}
-function checkScriptSyntax(source, filename) {
+function checkSyntax(source, filename) {
// Remove Shebang.
source = stripShebang(source);
+
+ const { getOptionValue } = require('internal/options');
+ const experimentalModules = getOptionValue('--experimental-modules');
+ if (experimentalModules) {
+ let isModule = false;
+ if (filename === '[stdin]' || filename === '[eval]') {
+ isModule = getOptionValue('--entry-type') === 'module';
+ } else {
+ const resolve = require('internal/modules/esm/default_resolve');
+ const { format } = resolve(pathToFileURL(filename).toString());
+ isModule = format === 'module';
+ }
+ if (isModule) {
+ const { ModuleWrap } = internalBinding('module_wrap');
+ new ModuleWrap(source, filename);
+ return;
+ }
+ }
+
// Remove BOM.
source = stripBOM(source);
// Wrap it.
diff --git a/lib/internal/main/eval_stdin.js b/lib/internal/main/eval_stdin.js
index 2a2ef6d38a1fe8..4face9e61e7e64 100644
--- a/lib/internal/main/eval_stdin.js
+++ b/lib/internal/main/eval_stdin.js
@@ -7,6 +7,7 @@ const {
} = require('internal/bootstrap/pre_execution');
const {
+ evalModule,
evalScript,
readStdin
} = require('internal/process/execution');
@@ -16,5 +17,8 @@ markBootstrapComplete();
readStdin((code) => {
process._eval = code;
- evalScript('[stdin]', process._eval, process._breakFirstLine);
+ if (require('internal/options').getOptionValue('--entry-type') === 'module')
+ evalModule(process._eval);
+ else
+ evalScript('[stdin]', process._eval, process._breakFirstLine);
});
diff --git a/lib/internal/main/eval_string.js b/lib/internal/main/eval_string.js
index 953fab386d0671..b0322819251e76 100644
--- a/lib/internal/main/eval_string.js
+++ b/lib/internal/main/eval_string.js
@@ -6,11 +6,15 @@
const {
prepareMainThreadExecution
} = require('internal/bootstrap/pre_execution');
-const { evalScript } = require('internal/process/execution');
+const { evalModule, evalScript } = require('internal/process/execution');
const { addBuiltinLibsToObject } = require('internal/modules/cjs/helpers');
-const source = require('internal/options').getOptionValue('--eval');
+const { getOptionValue } = require('internal/options');
+const source = getOptionValue('--eval');
prepareMainThreadExecution();
addBuiltinLibsToObject(global);
markBootstrapComplete();
-evalScript('[eval]', source, process._breakFirstLine);
+if (getOptionValue('--entry-type') === 'module')
+ evalModule(source);
+else
+ evalScript('[eval]', source, process._breakFirstLine);
diff --git a/lib/internal/main/repl.js b/lib/internal/main/repl.js
index e6b98853511d11..c2bf54f8bb6981 100644
--- a/lib/internal/main/repl.js
+++ b/lib/internal/main/repl.js
@@ -13,6 +13,12 @@ const {
prepareMainThreadExecution();
+// --entry-type flag not supported in REPL
+if (require('internal/options').getOptionValue('--entry-type')) {
+ console.error('Cannot specify --entry-type for REPL');
+ process.exit(1);
+}
+
const cliRepl = require('internal/repl');
cliRepl.createInternalRepl(process.env, (err, repl) => {
if (err) {
diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js
index 3f280dd4beac35..42f143be945175 100644
--- a/lib/internal/modules/cjs/loader.js
+++ b/lib/internal/modules/cjs/loader.js
@@ -872,9 +872,11 @@ Module.runMain = function() {
.catch((e) => {
internalBinding('task_queue').triggerFatalException(e);
});
- } else {
- Module._load(process.argv[1], null, true);
+ // Handle any nextTicks added in the first tick of the program
+ process._tickCallback();
+ return;
}
+ Module._load(process.argv[1], null, true);
// Handle any nextTicks added in the first tick of the program
process._tickCallback();
};
diff --git a/lib/internal/modules/esm/default_resolve.js b/lib/internal/modules/esm/default_resolve.js
index 33366f0069a8f1..8da24cf5b3afab 100644
--- a/lib/internal/modules/esm/default_resolve.js
+++ b/lib/internal/modules/esm/default_resolve.js
@@ -1,57 +1,54 @@
'use strict';
-const { URL } = require('url');
-const CJSmodule = require('internal/modules/cjs/loader');
const internalFS = require('internal/fs/utils');
const { NativeModule } = require('internal/bootstrap/loaders');
const { extname } = require('path');
const { realpathSync } = require('fs');
const { getOptionValue } = require('internal/options');
+
const preserveSymlinks = getOptionValue('--preserve-symlinks');
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
-const {
- ERR_MISSING_MODULE,
- ERR_MODULE_RESOLUTION_LEGACY,
- ERR_UNKNOWN_FILE_EXTENSION
-} = require('internal/errors').codes;
-const { resolve: moduleWrapResolve } = internalBinding('module_wrap');
-const StringStartsWith = Function.call.bind(String.prototype.startsWith);
+const experimentalJsonModules = getOptionValue('--experimental-json-modules');
+const typeFlag = getOptionValue('--entry-type');
+
+const { resolve: moduleWrapResolve,
+ getPackageType } = internalBinding('module_wrap');
const { pathToFileURL, fileURLToPath } = require('internal/url');
+const { ERR_ENTRY_TYPE_MISMATCH,
+ ERR_UNKNOWN_FILE_EXTENSION } = require('internal/errors').codes;
const realpathCache = new Map();
-function search(target, base) {
- if (base === undefined) {
- // We cannot search without a base.
- throw new ERR_MISSING_MODULE(target);
- }
- try {
- return moduleWrapResolve(target, base);
- } catch (e) {
- e.stack; // cause V8 to generate stack before rethrow
- let error = e;
- try {
- const questionedBase = new URL(base);
- const tmpMod = new CJSmodule(questionedBase.pathname, null);
- tmpMod.paths = CJSmodule._nodeModulePaths(
- new URL('./', questionedBase).pathname);
- const found = CJSmodule._resolveFilename(target, tmpMod);
- error = new ERR_MODULE_RESOLUTION_LEGACY(target, base, found);
- } catch {
- // ignore
- }
- throw error;
- }
-}
+// const TYPE_NONE = 0;
+const TYPE_COMMONJS = 1;
+const TYPE_MODULE = 2;
const extensionFormatMap = {
'__proto__': null,
- '.mjs': 'esm',
- '.json': 'json',
- '.node': 'addon',
- '.js': 'cjs'
+ '.cjs': 'commonjs',
+ '.js': 'module',
+ '.mjs': 'module'
};
+const legacyExtensionFormatMap = {
+ '__proto__': null,
+ '.cjs': 'commonjs',
+ '.js': 'commonjs',
+ '.json': 'commonjs',
+ '.mjs': 'module',
+ '.node': 'commonjs'
+};
+
+if (experimentalJsonModules) {
+ // This is a total hack
+ Object.assign(extensionFormatMap, {
+ '.json': 'json'
+ });
+ Object.assign(legacyExtensionFormatMap, {
+ '.json': 'json'
+ });
+}
+
function resolve(specifier, parentURL) {
if (NativeModule.canBeRequiredByUsers(specifier)) {
return {
@@ -60,21 +57,11 @@ function resolve(specifier, parentURL) {
};
}
- let url;
- try {
- url = search(specifier,
- parentURL || pathToFileURL(`${process.cwd()}/`).href);
- } catch (e) {
- if (typeof e.message === 'string' &&
- StringStartsWith(e.message, 'Cannot find module')) {
- e.code = 'MODULE_NOT_FOUND';
- // TODO: also add e.requireStack to match behavior with CJS
- // MODULE_NOT_FOUND.
- }
- throw e;
- }
-
const isMain = parentURL === undefined;
+ if (isMain)
+ parentURL = pathToFileURL(`${process.cwd()}/`).href;
+
+ let url = moduleWrapResolve(specifier, parentURL);
if (isMain ? !preserveSymlinksMain : !preserveSymlinks) {
const real = realpathSync(fileURLToPath(url), {
@@ -86,19 +73,40 @@ function resolve(specifier, parentURL) {
url.hash = old.hash;
}
+ const type = getPackageType(url.href);
+
const ext = extname(url.pathname);
+ const extMap =
+ type !== TYPE_MODULE ? legacyExtensionFormatMap : extensionFormatMap;
+ let format = extMap[ext];
- let format = extensionFormatMap[ext];
+ if (isMain && typeFlag) {
+ // Conflict between explicit extension (.mjs, .cjs) and --entry-type
+ if (ext === '.cjs' && typeFlag === 'module' ||
+ ext === '.mjs' && typeFlag === 'commonjs') {
+ throw new ERR_ENTRY_TYPE_MISMATCH(
+ fileURLToPath(url), ext, typeFlag, 'extension');
+ }
+
+ // Conflict between package scope type and --entry-type
+ if (ext === '.js') {
+ if (type === TYPE_MODULE && typeFlag === 'commonjs' ||
+ type === TYPE_COMMONJS && typeFlag === 'module') {
+ throw new ERR_ENTRY_TYPE_MISMATCH(
+ fileURLToPath(url), ext, typeFlag, 'scope');
+ }
+ }
+ }
if (!format) {
- if (isMain)
- format = 'cjs';
+ if (isMain && typeFlag)
+ format = typeFlag;
+ else if (isMain)
+ format = type === TYPE_MODULE ? 'module' : 'commonjs';
else
- throw new ERR_UNKNOWN_FILE_EXTENSION(url.pathname);
+ throw new ERR_UNKNOWN_FILE_EXTENSION(fileURLToPath(url),
+ fileURLToPath(parentURL));
}
-
return { url: `${url}`, format };
}
module.exports = resolve;
-// exported for tests
-module.exports.search = search;
diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js
index aff12113688b44..465775f56ffc72 100644
--- a/lib/internal/modules/esm/loader.js
+++ b/lib/internal/modules/esm/loader.js
@@ -11,10 +11,12 @@ const { URL } = require('url');
const { validateString } = require('internal/validators');
const ModuleMap = require('internal/modules/esm/module_map');
const ModuleJob = require('internal/modules/esm/module_job');
+
const defaultResolve = require('internal/modules/esm/default_resolve');
const createDynamicModule = require(
'internal/modules/esm/create_dynamic_module');
-const translators = require('internal/modules/esm/translators');
+const { translators } = require('internal/modules/esm/translators');
+const { ModuleWrap } = internalBinding('module_wrap');
const FunctionBind = Function.call.bind(Function.prototype.bind);
@@ -32,6 +34,9 @@ class Loader {
// Registry of loaded modules, akin to `require.cache`
this.moduleMap = new ModuleMap();
+ // Map of already-loaded CJS modules to use
+ this.cjsCache = new Map();
+
// The resolver has the signature
// (specifier : string, parentURL : string, defaultResolve)
// -> Promise<{ url : string, format: string }>
@@ -48,6 +53,8 @@ class Loader {
// an object with the same keys as `exports`, whose values are get/set
// functions for the actual exported values.
this._dynamicInstantiate = undefined;
+ // The index for assigning unique URLs to anonymous module evaluation
+ this.evalIndex = 0;
}
async resolve(specifier, parentURL) {
@@ -95,9 +102,25 @@ class Loader {
return { url, format };
}
+ async eval(source, url = `eval:${++this.evalIndex}`) {
+ const evalInstance = async (url) => {
+ return {
+ module: new ModuleWrap(source, url),
+ reflect: undefined
+ };
+ };
+ const job = new ModuleJob(this, url, evalInstance, false);
+ this.moduleMap.set(url, job);
+ const { module, result } = await job.run();
+ return {
+ namespace: module.namespace(),
+ result
+ };
+ }
+
async import(specifier, parent) {
const job = await this.getModuleJob(specifier, parent);
- const module = await job.run();
+ const { module } = await job.run();
return module.namespace();
}
@@ -143,4 +166,4 @@ class Loader {
Object.setPrototypeOf(Loader.prototype, null);
-module.exports = Loader;
+exports.Loader = Loader;
diff --git a/lib/internal/modules/esm/module_job.js b/lib/internal/modules/esm/module_job.js
index 016495096c3b33..5666032df1d6d8 100644
--- a/lib/internal/modules/esm/module_job.js
+++ b/lib/internal/modules/esm/module_job.js
@@ -23,7 +23,7 @@ class ModuleJob {
// This is a Promise<{ module, reflect }>, whose fields will be copied
// onto `this` by `link()` below once it has been resolved.
- this.modulePromise = moduleProvider(url, isMain);
+ this.modulePromise = moduleProvider.call(loader, url, isMain);
this.module = undefined;
this.reflect = undefined;
@@ -101,8 +101,9 @@ class ModuleJob {
async run() {
const module = await this.instantiate();
- module.evaluate(-1, false);
- return module;
+ const timeout = -1;
+ const breakOnSigint = false;
+ return { module, result: module.evaluate(timeout, breakOnSigint) };
}
}
Object.setPrototypeOf(ModuleJob.prototype, null);
diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js
index de09910872e133..172569fb042c69 100644
--- a/lib/internal/modules/esm/translators.js
+++ b/lib/internal/modules/esm/translators.js
@@ -11,14 +11,10 @@ const internalURLModule = require('internal/url');
const createDynamicModule = require(
'internal/modules/esm/create_dynamic_module');
const fs = require('fs');
-const { _makeLong } = require('path');
const {
SafeMap,
- JSON,
- FunctionPrototype,
- StringPrototype
} = primordials;
-const { URL } = require('url');
+const { fileURLToPath, URL } = require('url');
const { debuglog } = require('internal/util/debuglog');
const { promisify } = require('internal/util');
const esmLoader = require('internal/process/esm_loader');
@@ -26,14 +22,13 @@ const {
ERR_UNKNOWN_BUILTIN_MODULE
} = require('internal/errors').codes;
const readFileAsync = promisify(fs.readFile);
-const readFileSync = fs.readFileSync;
-const StringReplace = FunctionPrototype.call.bind(StringPrototype.replace);
+const StringReplace = Function.call.bind(String.prototype.replace);
const JsonParse = JSON.parse;
const debug = debuglog('esm');
const translators = new SafeMap();
-module.exports = translators;
+exports.translators = translators;
function initializeImportMeta(meta, { url }) {
meta.url = url;
@@ -45,7 +40,7 @@ async function importModuleDynamically(specifier, { url }) {
}
// Strategy for loading a standard JavaScript module
-translators.set('esm', async (url) => {
+translators.set('module', async function moduleStrategy(url) {
const source = `${await readFileAsync(new URL(url))}`;
debug(`Translating StandardModule ${url}`);
const module = new ModuleWrap(stripShebang(source), url);
@@ -62,9 +57,14 @@ translators.set('esm', async (url) => {
// Strategy for loading a node-style CommonJS module
const isWindows = process.platform === 'win32';
const winSepRegEx = /\//g;
-translators.set('cjs', async (url, isMain) => {
+translators.set('commonjs', async function commonjsStrategy(url, isMain) {
debug(`Translating CJSModule ${url}`);
const pathname = internalURLModule.fileURLToPath(new URL(url));
+ const cached = this.cjsCache.get(url);
+ if (cached) {
+ this.cjsCache.delete(url);
+ return cached;
+ }
const module = CJSModule._cache[
isWindows ? StringReplace(pathname, winSepRegEx, '\\') : pathname];
if (module && module.loaded) {
@@ -84,7 +84,7 @@ translators.set('cjs', async (url, isMain) => {
// Strategy for loading a node builtin CommonJS module that isn't
// through normal resolution
-translators.set('builtin', async (url) => {
+translators.set('builtin', async function builtinStrategy(url) {
debug(`Translating BuiltinModule ${url}`);
// Slice 'node:' scheme
const id = url.slice(5);
@@ -103,31 +103,38 @@ translators.set('builtin', async (url) => {
});
});
-// Strategy for loading a node native module
-translators.set('addon', async (url) => {
- debug(`Translating NativeModule ${url}`);
- return createDynamicModule(['default'], url, (reflect) => {
- debug(`Loading NativeModule ${url}`);
- const module = { exports: {} };
- const pathname = internalURLModule.fileURLToPath(new URL(url));
- process.dlopen(module, _makeLong(pathname));
- reflect.exports.default.set(module.exports);
- });
-});
-
// Strategy for loading a JSON file
-translators.set('json', async (url) => {
+translators.set('json', async function jsonStrategy(url) {
debug(`Translating JSONModule ${url}`);
- return createDynamicModule(['default'], url, (reflect) => {
- debug(`Loading JSONModule ${url}`);
- const pathname = internalURLModule.fileURLToPath(new URL(url));
- const content = readFileSync(pathname, 'utf8');
- try {
- const exports = JsonParse(stripBOM(content));
+ debug(`Loading JSONModule ${url}`);
+ const pathname = fileURLToPath(url);
+ const modulePath = isWindows ?
+ StringReplace(pathname, winSepRegEx, '\\') : pathname;
+ let module = CJSModule._cache[modulePath];
+ if (module && module.loaded) {
+ const exports = module.exports;
+ return createDynamicModule(['default'], url, (reflect) => {
reflect.exports.default.set(exports);
- } catch (err) {
- err.message = pathname + ': ' + err.message;
- throw err;
- }
+ });
+ }
+ const content = await readFileAsync(pathname, 'utf-8');
+ try {
+ const exports = JsonParse(stripBOM(content));
+ module = {
+ exports,
+ loaded: true
+ };
+ } catch (err) {
+ // TODO (BridgeAR): We could add a NodeCore error that wraps the JSON
+ // parse error instead of just manipulating the original error message.
+ // That would allow to add further properties and maybe additional
+ // debugging information.
+ err.message = pathname + ': ' + err.message;
+ throw err;
+ }
+ CJSModule._cache[modulePath] = module;
+ return createDynamicModule(['default'], url, (reflect) => {
+ debug(`Parsing JSONModule ${url}`);
+ reflect.exports.default.set(module.exports);
});
});
diff --git a/lib/internal/process/esm_loader.js b/lib/internal/process/esm_loader.js
index 0b7f1be6ff0595..6225ea81ab1364 100644
--- a/lib/internal/process/esm_loader.js
+++ b/lib/internal/process/esm_loader.js
@@ -3,15 +3,15 @@
const {
callbackMap,
} = internalBinding('module_wrap');
+const {
+ ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING,
+} = require('internal/errors').codes;
+const { Loader } = require('internal/modules/esm/loader');
const { pathToFileURL } = require('internal/url');
-const Loader = require('internal/modules/esm/loader');
const {
wrapToModuleMap,
} = require('internal/vm/source_text_module');
-const {
- ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING,
-} = require('internal/errors').codes;
exports.initializeImportMetaObject = function(wrap, meta) {
if (callbackMap.has(wrap)) {
@@ -34,9 +34,7 @@ exports.importModuleDynamicallyCallback = async function(wrap, specifier) {
};
let loaderResolve;
-exports.loaderPromise = new Promise((resolve, reject) => {
- loaderResolve = resolve;
-});
+exports.loaderPromise = new Promise((resolve) => loaderResolve = resolve);
exports.ESMLoader = undefined;
diff --git a/lib/internal/process/execution.js b/lib/internal/process/execution.js
index 7118dbf3ad50d2..070410ef6faf21 100644
--- a/lib/internal/process/execution.js
+++ b/lib/internal/process/execution.js
@@ -33,6 +33,24 @@ function tryGetCwd() {
}
}
+function evalModule(source) {
+ const { decorateErrorStack } = require('internal/util');
+ const asyncESM = require('internal/process/esm_loader');
+ asyncESM.loaderPromise.then(async (loader) => {
+ const { result } = await loader.eval(source);
+ if (require('internal/options').getOptionValue('--print')) {
+ console.log(result);
+ }
+ })
+ .catch((e) => {
+ decorateErrorStack(e);
+ console.error(e);
+ process.exit(1);
+ });
+ // Handle any nextTicks added in the first tick of the program.
+ process._tickCallback();
+}
+
function evalScript(name, body, breakFirstLine) {
const CJSModule = require('internal/modules/cjs/loader');
const { kVmBreakFirstLineSymbol } = require('internal/util');
@@ -176,6 +194,7 @@ function readStdin(callback) {
module.exports = {
readStdin,
tryGetCwd,
+ evalModule,
evalScript,
fatalException: createFatalException(),
setUncaughtExceptionCaptureCallback,
diff --git a/src/env.h b/src/env.h
index 55018b6d40d1ca..743c236827b07d 100644
--- a/src/env.h
+++ b/src/env.h
@@ -77,11 +77,13 @@ struct PackageConfig {
enum class Exists { Yes, No };
enum class IsValid { Yes, No };
enum class HasMain { Yes, No };
+ enum PackageType : uint32_t { None = 0, CommonJS, Module };
- Exists exists;
- IsValid is_valid;
- HasMain has_main;
- std::string main;
+ const Exists exists;
+ const IsValid is_valid;
+ const HasMain has_main;
+ const std::string main;
+ const PackageType type;
};
} // namespace loader
@@ -141,6 +143,7 @@ constexpr size_t kFsStatsBufferLength = kFsStatsFieldsNumber * 2;
V(channel_string, "channel") \
V(chunks_sent_since_last_write_string, "chunksSentSinceLastWrite") \
V(code_string, "code") \
+ V(commonjs_string, "commonjs") \
V(config_string, "config") \
V(constants_string, "constants") \
V(crypto_dsa_string, "dsa") \
diff --git a/src/module_wrap.cc b/src/module_wrap.cc
index 56149d0cc759c9..622deed45ff668 100644
--- a/src/module_wrap.cc
+++ b/src/module_wrap.cc
@@ -29,7 +29,6 @@ using v8::HandleScope;
using v8::Integer;
using v8::IntegrityLevel;
using v8::Isolate;
-using v8::JSON;
using v8::Just;
using v8::Local;
using v8::Maybe;
@@ -46,7 +45,13 @@ using v8::String;
using v8::Undefined;
using v8::Value;
-static const char* const EXTENSIONS[] = {".mjs", ".js", ".json", ".node"};
+static const char* const EXTENSIONS[] = {
+ ".mjs",
+ ".cjs",
+ ".js",
+ ".json",
+ ".node"
+};
ModuleWrap::ModuleWrap(Environment* env,
Local