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 -

Comments 14

Offline
tech
tech 10 March 2020 12:20
Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post. נופש בישראל


I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. נופש בישראל
Offline
tech
tech 10 March 2020 14:22
I am impressed. I don't think Ive met anyone who knows as much about this subject as you do. You are truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. Really, great blog you have got here. חבילות נופש בפסח
Offline
Rank Feed
Rank Feed 11 March 2020 12:36
Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info.doctor de migraña en houston

Offline
tech
tech 12 March 2020 13:42
Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work.חופשה באירופה

Offline
tech
tech 14 March 2020 11:22
Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking.בתי מלון באילת

Offline
tech
tech 14 March 2020 13:31
I am impressed. I don't think Ive met anyone who knows as much about this subject as you do. You are truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. Really, great blog you have got here.בתי מלון בצפון

Offline
tech
tech 15 March 2020 14:49
Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking.חבילות לקפריסין



Very efficiently written information. It will be beneficial to anybody who utilizes it, including me. Keep up the good work. For sure i will check out more posts. This site seems to get a good amount of visitors.חבילות ליוון

Offline
tech
tech 16 March 2020 13:30
Hello, this weekend is good for me, since this time i am reading this enormous informative article here at my home.score88poker

Offline
tech
tech 18 March 2020 14:48
This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here keep up the good workbest online loans instant approval from mmPersonalLoans
Offline
Rank Feed
Rank Feed 19 March 2020 12:47
Hello, this weekend is good for me, since this time i am reading this enormous informative article here at my home.One Pearl Bank

Offline
tech
tech 19 March 2020 15:27
This is my first time visit to your blog and I am very interested in the articles that you serve. Provide enough knowledge for me. Thank you for sharing useful and don't forget, keep sharing useful info:500$ loan from UnitedFinances.com

Offline
Seo Expret
Seo Expret 20 March 2020 09:46
Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info.Kampala

Offline
tech
tech 22 March 2020 11:52
Hello, this weekend is good for me, since this time i am reading this enormous informative article here at my home.Avenue South Residence

Offline
Tech
Tech 23 March 2020 18:33
Wow, What a Excellent post. I really found this to much informatics. It is what i was searching for.I would like to suggest you that please keep sharing such type of info.ThanksHyll On Holland

Add comment