ECMAScript Modules in Node.js: New Plan

 
3r3-31.
The current status of support for ECMAScript modules (ESM) in Node.js is
3r3303.  
3r3305.  
3r3333317. Experimental support for ESM was added in r3r311. Node.js ???
September 1? 2017.
 
3r3333317. After that, the Technical Steering Committee of Node.js formed a team responsible for the modules (3r3166. Modules Team
) To help design the missing parts for the upcoming (non-experimental) release. This team consists of people from different branches of web development (frontend, backend, JS engines, etc.).
 
3r3303.  
In October, the Modules Team published 3r3308. "Implementation Plan for New Modules"
. This post explains what it contains.
"browser equivalence" : Node.js should be as close to the browsers as possible. The kernel achieves this by simplifying the parsing of module identifiers (URLs pointing to modules): 3r-3298. 3r3303.  
3r3305.  
3r3333317. Each module identifier must end with a file name with extension. That is,
 
3r3305.  
3r3333317. Extensions are not added automatically
 
3r3333317. Directory importing is not supported (either via redirection to 3r33333. Dir /index.mjs , Or reading the 3r33333 field. Main To 3r33332. Package.json ).
 
 
3r3333317. ES modules can import embedded Node.js modules (3r33333. Path And the like). They are the only exception to the previous rule.
 
3r3333317. By default, only files with a extension are supported. .mjs (see Phase 2 if you are interested in the status of other extensions). Thus, other types of modules cannot be imported through 3r33333. import : CommonJS modules, JSON files, native modules.  
3r3303.  
3r3105. Bringing important CommonJS features to ES modules 3r3303.  
3r3305.  
3r3333317. The URL of the current module (similar to __Filename From CommonJS): import.meta.url  
3r3333317. Dynamic import of ES modules (available through Require () In CommonJS): import () operator 3r33333.  
3r3303.  
3r33130. Compatible 3r3131. 3r3303.  
3r3305.  
3r3333317. ES modules will be able to import CommonJS modules via
createRequireFromPath () 3r33333. . This will work as follows (there are plans to make an abbreviated method, for example, the function CreateRequireFromUrl () ):  
3r3303.  
3r3148. 3r3149. import {createRequireFromPath as createRequire} from 'module';
import {fileURLToPath as fromPath} from 'url';
const require = createRequire (fromPath (import.meta.url));
const cjsModule = require ('./cjs-module.js'); 3r3303.  
3r3305.  
3r3333317. CommonJS modules will be able to load ES modules via 3r33333. import () .  
3r3303.  
3r3168. Phase 2 and Future Plans 3r3302. 3r3303.  
3r3305.  
3r3333317. In the second phase, waiting for us: 3r3303.  
3r3305.  
3r3333317. Support for "bare" identifiers, such as 'lodash' . Most likely, this will include some way of mapping these identifiers into real paths.  
3r3333317. Support for file extensions other than .mjs . This includes including support for ES modules in 3r323333. .js files.  
 
3r3333317. Phase 3 is likely to focus on module loaders with extension points, in which users can connect their logic.  
3r3303.  

When can I use ES modules in Node.js? 3r3302. 3r3303.  
3r3305.  
3r3333317. For the flag: 3r3207. Available now
3r3303.  
3r3305.  
3r3333317. Warning: the behavior is not as described above in Phase 1 (as of Node.js ???) 3r320.  
 
3r3333317. Without a flag and in accordance with phase 1: Modules The team tries to make this possible as soon as possible. We hope that the modules will be released from under the flag in Node.js 14 (April 2020) and will be ported to previous versions, if possible.  
3r3303.  

Frequently Asked Questions 3r3302. 3r3303.  
3r3305.  
3r3333317. Why do you need a new file extension .mjs ?
3r3303.  
3r3305.  
3r3333317. Each solution for distinguishing between ESM and CommonJS formats has its advantages and disadvantages. Using a separate resolution seems like a good option (3r3-33241. More info 3r-3319.).
 
 
3r3333317.
Why should the behavior of Node.js look like a browser?
3r3303.  
3r3305.  
3r3333317. Because it makes it possible to reuse the code. For example, to create libraries that work simultaneously in browsers and in Node.js
 
3r3333317. It should also facilitate switching between the backend and the frontend during coding.
 
 
3r3333317.
Why all these limitations for compatibility?
3r3303.  
3r3305.  
3r3333317. Between ES and CommonJS-modules there are quite strong differences in the structure (static versus dynamic) and the method of loading (asynchronous versus synchronous). Restrictions help keep things simple - given that in the long run, the overwhelming majority will be ES modules.
 
 
3r3333317.
Why is it all lasting so long?
3r3303.  
3r3305.  
3r3333317. There are many stakeholders involved and many different platforms involved (Node.js, npm, browsers, JS engines, TypeScript, TC3? and others). If we really get ES-modules that can work everywhere, it's probably worth the wait, IMHO.
 
 
3r3303.  
Acknowledgment 3r3302. 3r3303.  
Thanks to Miles Borins for his feedback on this post.
3r3303.  
3r33333. Additional sources for further reading 3r3302. 3r3303.  
3r3305.  
3r3333317. 3r3308. "Implementation Plan for New Modules"
from Node.js Foundation Modules Team
 
3r3333317. Chapter 3r33313. "Modules"
from "jаvascript for impatient programmers" (explains the features of scripts, CommonJS and ES modules) 3r320.  
3r3333317. The initial behavior of the experimental version of ESM in Node.js is described in 3r33318. my previous post
.
 
3r33333.
3r33333.

+ 0 -

Add comment