介绍


¥Introduction

ECMAScript 模块是 官方标准格式,用于打包 JavaScript 代码以供重用。模块使用各种 importexport 语句定义。

¥ECMAScript modules are the official standard format to package JavaScript code for reuse. Modules are defined using a variety of import and export statements.

以下是 ES 模块导出函数的示例:

¥The following example of an ES module exports a function:

// addTwo.mjs
function addTwo(num) {
  return num + 2;
}

export { addTwo }; 

以下是 ES 模块从 addTwo.mjs 导入函数的示例:

¥The following example of an ES module imports the function from addTwo.mjs:

// app.mjs
import { addTwo } from './addTwo.mjs';

// Prints: 6
console.log(addTwo(4)); 

Node.js 完全支持当前指定的 ECMAScript 模块,并提供它们与其原始模块格式 CommonJS 之间的互操作性。

¥Node.js fully supports ECMAScript modules as they are currently specified and provides interoperability between them and its original module format, CommonJS.