This article was originally published on the public account CoyPan as expected
Writing in the front
Node.js recently announced support for ES Modules with the release of V13.2.0. Before that, to use ES modules in Node, add — experimental-Module. With v13.2.0, you can use ES Modules directly.
Use ES modules in Node
To use ES Modules in a project, there are two ways:
1. Use file name extensions.mjs
For example, suppose the project directory looks like this:
.
|____component
| |____a.mjs
|____index.mjs
Copy the code
The content of the document is as follows:
// component/a.mjs
let a = 'This is component a';
export default a;
// index.mjs
import a from './component/a.mjs';
console.log(a);
Copy the code
Execute node index. MJS, the code executes correctly, and output looks like this:
(node:77465) ExperimentalWarning: The ESM module loader is experimental.
This is component a
Copy the code
2. Files are still used.js
The suffix name in the itempackage.json
In the Settings:type:module
The project catalog is as follows:
.
|____component
| |____a.js
|____package.json
|____index.js
Copy the code
The contents of each file are as follows:
// component/a.js
let a = 'This is component a';
export default a;
// index.js
import a from './component/a.mjs';
console.log(a);
// package.json{..."type": "module".// There must be this line. }Copy the code
Execute node index.js, and the code executes correctly, with the following output:
(node:78977) ExperimentalWarning: The ESM module loader is experimental.
This is component a
Copy the code
If ES module is used on the command line, add –input-type=module. Here’s an example:
node --input-type=module --eval "import { sep } from 'path'; console.log(sep);"
Copy the code
It should be noted that at present, the implementation of ES Module is still experimental, and it may be adjusted at any time in the future.
Import
When importing an ES Module, you can use the following methods:
- Relative path (
./file.mjs
) - Absolute path (
file:///opt/app/file.mjs
) - Module name (
Es - module - package '
) - Intra-module path (
es-module-package/lib/file.mjs
)
Alternatively, when using import to reference ES Module, you can use it like this:
The import _ the from 'es - module - package'
Import {shuffle} from 'es-module-package
Import * as fs from 'fs'
All Node built-in modules, such as FS and PATH, can be referenced in the preceding three ways.
The import and Commonjs
Import can also import Commonjs modules, including the following two cases:
- The module is written using the Commonjs specification and has a suffix named:
.cjs
For example, the code directory is as follows:
.
|____component
| |____a.cjs
|____package.json
|____index.js
Copy the code
The content of the document is as follows:
// component/a.cjs
let a = 'This is component a';
console.log('aaaa');
module.exports = a;
// index.js
import a from './component/a.cjs';
console.log(a);
// package.json{..."type": "module". }Copy the code
To execute node index.js, the code should execute correctly, enter:
(node:81677) ExperimentalWarning: The ESM module loader is experimental.
aaaa
This is component a
Copy the code
- The module has its own package.json, with no Settings
"type": "module"
Or set"type": "commonjs"
. For example, the code directory is as follows:
.
|____component
| |____a.js
| |____package.json
|____package.json
|____index.js
Copy the code
The content of the document is as follows:
// component/a.js
let a = 'This is component a';
console.log('aaaa');
module.exports = a;
// component/package.json
{
"type": "commonjs" // The type field is optional
}
// index.js
import a from './component/a.js';
console.log(a);
// package.json{..."type": "module". }Copy the code
To execute node index.js, the code should execute correctly, enter:
(node:81677) ExperimentalWarning: The ESM module loader is experimental.
aaaa
This is component a
Copy the code
Write in the back
This article briefly introduces how to use ES Module in Node.js through examples. For more details, please refer to the official documentation:
Nodejs.org/api/esm.htm…