ECMAScript modules are [the official standard format][] to package JavaScript
code for reuse. Modules are defined using a variety of [import
][] and
[export
][] statements.
The following example of an ES module exports a function:
// addTwo.mjs
function addTwo(num) {
return num + 2;
}
export { addTwo };
The following example of an ES module imports the function from addTwo.mjs
:
// app.mjs
import { addTwo } from './addTwo.mjs';
// Prints: 6
console.log(addTwo(4));
Node.js fully supports ECMAScript modules as they are currently specified and provides 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.