Announcing a new --experimental-modules

Photo by Brett Sayles from Pexels

What’s in --experimental-modules

Like the previous version, this new --experimental-modules adds support to Node.js for the following:

  • ES2015 import statements can reference JavaScript files that are themselves written using ES module syntax. Files can be referenced as relative URLs (‘./file.mjs’), absolute file:// URLs (‘file:///opt/app/file.mjs’), package names (‘es-module-package’) or paths within packages (‘es-module-package/lib/file.mjs’).
  • import statements that reference ES module files can specify both the default export (import _ from ‘es-module-package’), named exports (import { shuffle } from ‘es-module-package’) and namespace exports (import * as fs from ‘fs’). All Node.js built-in packages like fs and path support all three types of exports.
  • import statements that reference CommonJS files (all current JavaScript code written for Node.js, using require and module.exports) can use the CommonJS default export (import _ from ‘commonjs-package’) only. This is a work in progress and may change in the future.
  • export statements in ES module files can specify both default and named exports for import statements to reference.
  • Dynamic import() expressions can be used to import ES modules from either CommonJS or ES module files. Note that import() returns a promise.
  • import.meta.url provides the absolute URL of the current ES module file.
  • Loaders can be written to modify Node.js’s runtime behavior with respect to ES modules. This is still very much a work in progress.
  • Node.js can be run with an ES module file as a program’s initial entry point.
  • Files loaded as ES modules are loaded in strict mode, which in CommonJS requires adding ‘use strict’; to the top of every file.
  • Files ending in .mjs are explicitly treated as ES modules in import statements and when run via the node command.

import and export syntax in .js files

We heard some very strong feedback that Node.js needs to provide a way to use import and export syntax in .js files.

package.json “type” field

Add “type”: “module” to the package.json for your project, and Node.js will treat all .js files in your project as ES modules.

--input-type flag

Use --input-type=module to run string input (via --eval, --print or STDIN) as an ES module. The --input-type flag can be --input-type=module or --input-type=commonjs.

.cjs extension

Just as the .mjs file extension explicitly signifies that a file should be treated as an ES module, the new .cjs file extension explicitly signifies that a file should be treated as CommonJS. (CommonJS is the other module system that Node.js supports, with require and module.exports.) The .cjs extension provides a way to save CommonJS files in a project where both .mjs and .js files are treated as ES modules.

Explicit filenames

By default in the new --experimental-modules, file extensions are mandatory in import statements: import ‘./file.js’, not import ‘./file’. However, the CommonJS-style automatic extension resolution behavior (‘./file’) can be enabled via a new flag, --es-module-specifier-resolution=node. (Its inverse, the default, is --es-module-specifier-resolution=explicit.) Package names are still just package names, e.g. import fs from ‘fs’.


The “CommonJS globals” (require, exports, module, __filename, __dirname) are not defined in ES modules. However, module.createRequireFromPath() can be used to create a CommonJS require function to be used in an ES module context.

import for JavaScript only

The previous --experimental-modules allowed import statements of JSON and native modules. This has been removed; you may use module.createRequireFromPath() for these.

ES module code in packages

This is a work in progress and subject to change. You can create a package with ES module sources by using the package.json “main” field to point to an ES module package entry point. Node.js will know to load it as an ES module if the file ends in .mjs or if the package.json also contains “type”: “module”.

Works in progress

All of the above is shipping as part of --experimental-modules in Node.js 12. On our road map for potential improvements before the --experimental-modules flag is hopefully dropped in October 2019, when Node.js 12 reaches LTS status:

  • Loaders features. Loaders are being further developed to support process isolation, multiple loaders, and multi-Realm support with lower-level hooks. The --loader API will still change considerably before this is unflagged.
  • Dual CommonJS/ES module packages. We want to provide a standard way for package authors to publish a package that can be both required into CommonJS or imported into an ES module.
  • Easier require. Using Module.createRequireFromPath() involves a lot of boilerplate. We hope to provide a simpler way to use require in ES module code.
  • Package path maps. We would like to support paths to files within packages. This would allow things like import sdk from ‘some-service/sdk’ to have ‘some-service/sdk’ map to something like ./src/sdk/public-api.mjs.
  • Automatic entry point module type detection. This would provide a way for running JavaScript code as either CommonJS or ES modules based on static analysis.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store



Node.js is a collaborative open source project dedicated to building and supporting the Node.js platform.