Node.js package.json 字段定义


本节描述了 Node.js 运行时使用的字段。 其他工具(例如 npm)使用 Node.js 忽略且未在此处记录的其他字段。

package.json 文件中的以下字段在 Node.js 中使用:

  • "name" - 当包中使用命名导入时相关。 也被包管理器用作包的名称。
  • "type" - 决定是否将 .js 文件加载为 CommonJS 或 ES 模块的包类型。
  • "exports" - 包导出和条件导出。 当存在时,限制可以从包中加载哪些子模块。
  • "main" - 加载包时的默认模块,如果没有指定导出,并且在引入导出之前的 Node.js 版本中。
  • "imports" - 包导入,供包本身内的模块使用。

This section describes the fields used by the Node.js runtime. Other tools (such as npm) use additional fields which are ignored by Node.js and not documented here.

The following fields in package.json files are used in Node.js:

  • "name" - Relevant when using named imports within a package. Also used by package managers as the name of the package.
  • "type" - The package type determining whether to load .js files as CommonJS or ES modules.
  • "exports" - Package exports and conditional exports. When present, limits which submodules can be loaded from within the package.
  • "main" - The default module when loading the package, if exports is not specified, and in versions of Node.js prior to the introduction of exports.
  • "imports" - Package imports, for use by modules within the package itself.