Home  >  Article  >  Web Front-end  >  Can the es module be executed in node?

Can the es module be executed in node?

WBOY
WBOYOriginal
2022-07-04 17:08:211733browse

The es module can be executed in node; node has supported the native es module since version 8.5.2. This function can be turned on through the command line option. The es module, also known as the ECMAScript module, is a JavaScript code reuse mechanism. , but require() cannot be used in the es module.

Can the es module be executed in node?

The operating environment of this article: Windows 10 system, nodejs version 12.19.0, Dell G3 computer.

es modules can be executed in node

Starting from version 8.5.0, Node.js begins to support native ES modules, and this feature can be turned on through command line options. The new features are largely due to Bradley Farias.

1. Demonstration

The code directory structure of this example is as follows:

esm-demo/
 lib.mjs
 main.mjs

lib.mjs:

export function add(x, y) {
 return x + y;
}

main.mjs:

import {add} from './lib.mjs';
console.log('Result: '+add(2, 3));

Run the demo:

$ node --experimental-modules main.mjs
Result: 5

2. Checklist: Things to note

ES module:

·You cannot import modules dynamically. However, work on dynamic import() is in progress and support should be available soon.

·No meta variables such as __dirname and __filename. However, there is a proposal for a similar function: "import.meta". It might look like this:

console.log(import.meta.url);

·Now all module identifiers are URLs (this part is new in Node.js) :

·File - relative path with file extension: ../util/tools.mjs

·Library - no file extension and no path lodash

· How to better make npm libraries available in browsers (without using bundler) remains to be seen. One possibility is to introduce RequireJS-style configuration data that maps paths to actual paths. Currently, it is illegal to use bare path module identifiers in browsers.

Interoperability with CJS modules

You can import CJS modules, but they always only have the default export - that is, the value of module.exports. Making CJS modules support named exports is already being worked on, but may take a while. If you can help, you can do it.

import fs1 from 'fs';
console.log(Object.keys(fs1).length); // 86
import * as fs2 from 'fs';
console.log(Object.keys(fs2)); // ['default']

· You cannot use require() in ES modules. The main reason is:

· Path resolution works slightly differently: ESM does not support NODE_PATH and require.extensions. Also, the fact that its identifier is always a URL causes some slight differences.

· ES modules are always loaded asynchronously, which ensures maximum compatibility with the web. This loading style cannot be mixed with synchronous loading of CJS modules via require().

· Disabling synchronous module loading also leaves a fallback path for Top-level await imports of ES modules (a feature currently under consideration).

Recommended learning: "nodejs video tutorial"

The above is the detailed content of Can the es module be executed in node?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What does nodelist mean?Next article:What does nodelist mean?