Node.js Plans to Support ES6 Modules: An In-Depth Look
The Node.js JavaScript runtime environment has long supported CommonJS modules for loading external code. However, with the advent of ES6 (ECMAScript 2015), the introduction of native module support sparked a question: would Node.js embrace import/export modules?
Node.js 13.2.0 and Beyond
As of Node.js 13.2.0, support for ES Modules has been introduced without requiring an experimental flag. Enabling ECMAScript module (ESM) support involves setting the package.json file to:
{ "type": "module" }
All files with .js, .mjs, or no extension will be treated as ESMs. Alternative methods of enabling ES Modules are also available and documented in the release notes.
Node.js 13.1.0 and Earlier
For older Node.js versions, the [esm](https://github.com/standard-things/esm) module loader provides a production-ready implementation of ES Modules:
node -r esm main.js
Historical Development Timeline
April 23, 2019
An update to the detection mechanism for ECMAScript modules was integrated, introducing options for package.type (module or commonjs) and new options to specify entry point types and module specifier resolution.
January 17, 2019
Node.js 11.6.0 included ES Modules as an experimental feature, enabled using the flag:
node --experimental-modules index.mjs
The plan was to remove this flag in the v10.0 LTS release.
September 13, 2017
Node.js 8.5.0 added support for .mjs files, accessible behind a flag:
node --experimental-modules index.mjs
The original intention was to remove this flag for the v10.0 LTS release.
September 8, 2017
Initial support for ESM modules was added to the Node.js master branch, made accessible behind the --experimental-modules flag.
February 2017
The Node.js team announced the decision to use the .mjs file extension as the least disruptive solution, allowing for coexistence of CommonJS and ES Modules. Implementation was estimated to take at least a year.
October 2016
After a TC-39 meeting, a Node.js developer outlined the challenges in implementing ES Modules. Key issues included static vs. dynamic analysis, monkey-patching, and module detection. The use of the *.mjs extension emerged as a likely solution.
Pre-2016
The debate over ES Module support in Node.js had been ongoing for a considerable period. Various proposals and discussions aimed to address the differing requirements of Node.js and HTML while accommodating the new specification.
The above is the detailed content of How has Node.js evolved its support for ES6 Modules?. For more information, please follow other related articles on the PHP Chinese website!