NodeJS v14 includes ES Module support unflagged. To use it you have to know
For backward-compat reasons, all packages are CommonJS by default. You can make your package an ES Modules package by putting
type: "module" in your package.json.
In a CommonJS package, all
.js files are cjs format by default and thus would use
require and other CommonJS idioms.
To use ESM in a CommonJS package, you must use the file extension
.mjs and then you can use
import in that file.
In an ESM packages, all
.js files are ESM format by default.
To use CommonJS in an ESM package, you must use the file extension
Regardless of which package type you use, you'll likely want to take advantage of conditional exports. Here we'll show the
require fields on the
import field indicates which file will be selected when using ESM
imports or dynamic
import() syntax. The
require field does the same for
require. This replaces
main for versions of node that support it, allowing you to support both commonjs usage and esm usage in a single package.
browser, as well as
exports are all used by various systems. It's kind of a mess and you should check what you're using. Webpack 5 for example, merged support for node.js conditional exports but has supported the
module key since version 2 (Rollup also has supported