2020-05-26 Nodejs V12.17.0 LTS release, remove — experimental-Modules flag.
Although supported in the latest LTS v12.17.0, it is currently stable: 1 – Experimental, if this function is used in the production environment, you should also be cautious, if you can install N install V12.17.0 in the test environment to try.
Removing the flags is also an important step towards making the ESM stable. According to the Nodejs release, it is expected to remove the warnings from Nodejs 12 later this year (around October), when Node 14 will become LTS.
ES Modules basic use
Use ES Modules either by declaring an.mjs file or by specifying type as module in package.json. Here are two ways to use them:
Mode of use 1
Build the following directory structure
├── ├.js ├─ package.jsonCopy the code
package.json
The point is to support ES Modules by setting type to module
{
"name": "esm-project". "version": "1.0.0". "main": "index.js". "type": "module"..} Copy the code
caculator.js
export function add (a, b) {
return a + b;
};
Copy the code
index.js
import { add } from './caculator.js';
console.log(add(4.2)); / / 6
Copy the code
run
Unlike current V14.3.0, using the ESM runtime in V12.17.0 still fires an experimental warning.
$n run v12.17.0 index.js (node:6827) ExperimentalWarning: The ESM Module loader is experimental. 6Copy the code
$n run v14.3.0 index.js 6
Mode of use 2
By specifying the file extension.mjs to distinguish it from CommonJS modules, you do not need to specify type module in package.json.
Modify the file name extension based on the previous example.
├ ─ ─ caculator. MJS ├ ─ ─ index. The MJSCopy the code
run
$n run v12.17.0 index.mjs (node:6827) ExperimentalWarning: The ESM Module loader is experimental. 6Copy the code
Module import and export several ways
Export export
Export is used for the external output module to export constants, functions, files, etc., which defines the external interface. There are two export methods:
- Export: The file is exported in export mode. When importing the file, add {} and know the names of the variables to be loaded in advance. You can use the file multiple times.
- Export default: specifies the default output for the module, so that the name of the loaded module variable does not need to be known during loading. It can be used only once in a file.
// caculator.js
export function add (a, b) {
return a + b;
};
export function subtract (a, b) { return a - b; } const caculator = { add, subtract, } export default caculator; Copy the code
The import import
The import statement is used to import the binding exported by another module in three ways:
- Import default value: Import the default interface defined in export Default.
- As alias import: You can rename the interface defined in export during import.
- Single or multiple imports: Import one or more interfaces specified by export as required.
import { add } from './caculator.js';
import caculator from './caculator.js';
import * as caculatorAs from './caculator.js';
add(4.2)
caculator.subtract(4.2); caculatorAs.subtract(4.2); Copy the code
Dynamic import of import
It is possible to import a module dynamically as if calling a function, which will return a Promise, but this way requires top-level await support, If you don’t know what top-level await is you can read this article Nodejs V14.3.0 release supports top-level await and REPL enhancements.
Now we have the following export module my-module.js:
const sleep = (value, ms) = > new Promise(resolve= > setTimeout((a)= > resolve(value), ms));
export const hello = await sleep('Hello'.1000);
export const node = await sleep('Nodejs'.2000);
export default function() {
return 'this is a module'; } Copy the code
Dynamic import can be done in index.js as follows:
console.log('Start loading module... ')
const myModule = await import('./my-module.js');
console.log('Output after 3000 ms.')
console.log(myModule.hello);
console.log(myModule.node);
console.log(myModule.default()); Copy the code
run
$n run v14.3.0 --experimental_top_level_await index.js Start loading module... Output after 3000 ms. Hello Nodejs this is a moduleCopy the code
2020-05-26 Nodejs V12.17.0 LTS version release, before this, if we use ES Modules also need to add the flag –experimental- Modules, but in this release, the flag is removed. This article is an introduction to using ES Modules in Nodejs, and I hope you’ll learn more about it in the future.
About the author: May Jun, software designer, moOCnet certified author, love technology, love to share the post-90s youth, welcome to Github open source project www.nodejs.red
Reference
- Nodejs.org/en/blog/rel…
- Nodejs.org/dist/latest…