package.json 和文件扩展名


¥package.json and file extensions

在包中,package.json "type" 字段定义了 Node.js 应该如何解释 .js 文件。如果 package.json 文件没有 "type" 字段,则 .js 文件将被视为 CommonJS

¥Within a package, the package.json "type" field defines how Node.js should interpret .js files. If a package.json file does not have a "type" field, .js files are treated as CommonJS.

"module"package.json "type" 值告诉 Node.js 将该包中的 .js 文件解释为使用 ES 模块 语法。

¥A package.json "type" value of "module" tells Node.js to interpret .js files within that package as using ES module syntax.

"type" 字段不仅适用于初始入口点 (node my-app.js),还适用于 import 语句和 import() 表达式引用的文件。

¥The "type" field applies not only to initial entry points (node my-app.js) but also to files referenced by import statements and import() expressions.

// my-app.js, treated as an ES module because there is a package.json
// file in the same folder with "type": "module".

import './startup/init.js';
// Loaded as ES module since ./startup contains no package.json file,
// and therefore inherits the "type" value from one level up.

import 'commonjs-package';
// Loaded as CommonJS since ./node_modules/commonjs-package/package.json
// lacks a "type" field or contains "type": "commonjs".

import './node_modules/commonjs-package/index.js';
// Loaded as CommonJS since ./node_modules/commonjs-package/package.json
// lacks a "type" field or contains "type": "commonjs". 

.mjs 结尾的文件总是加载为 ES 模块,而不管最近的父级 package.json

¥Files ending with .mjs are always loaded as ES modules regardless of the nearest parent package.json.

.cjs 结尾的文件总是加载为 CommonJS,而不管最近的父级 package.json

¥Files ending with .cjs are always loaded as CommonJS regardless of the nearest parent package.json.

import './legacy-file.cjs';
// Loaded as CommonJS since .cjs is always loaded as CommonJS.

import 'commonjs-package/src/index.mjs';
// Loaded as ES module since .mjs is always loaded as ES module. 

.mjs.cjs 扩展可用于在同一个包中混合类型:

¥The .mjs and .cjs extensions can be used to mix types within the same package:

  • "type": "module" 包中,可以指示 Node.js 通过将特定文件命名为 .cjs 扩展名将其解释为 CommonJS(因为 .js.mjs 文件都被视为 "module" 包中的 ES 模块)。

    ¥Within a "type": "module" package, Node.js can be instructed to interpret a particular file as CommonJS by naming it with a .cjs extension (since both .js and .mjs files are treated as ES modules within a "module" package).

  • "type": "commonjs" 包中,可以指示 Node.js 将特定文件解释为 ES 模块,方法是将其命名为 .mjs 扩展名(因为 .js.cjs 文件在 "commonjs" 包中都被视为 CommonJS)。

    ¥Within a "type": "commonjs" package, Node.js can be instructed to interpret a particular file as an ES module by naming it with an .mjs extension (since both .js and .cjs files are treated as CommonJS within a "commonjs" package).