"module" package.json 字段有什么用?

What is the "module" package.json field for?

我看到一些 npm 包 (vue for example) 在它们的 package.json 中有一个 module 字段。

但是 module is not included in the package.json documentation - 这是某种约定吗?某处有这方面的文档吗?

更新:2022 年 4 月

module 字段未由 Node.js 正式定义,因此未计划提供支持。相反,Node.js 社区选择了他们认为更通用的 package exports

出于实际原因 JavaScript 捆绑器将继续支持 module 字段。 esbuild 文档 explain 何时使用 module 以及相关字段 mainbrowser.

原答案

Is "module" an official npm property or is this a convention of some kind?

它是 proposal,但由于实际使用可能会得到支持。

Is there documentation for this somewhere?

其实有,而且可以找到right here and later removed here

What's it for?

Node.js 中的 ES6 模块互操作性可以从 Rich Harris 那里找到更多讨论 here and here. And here's a blog post 谈论更多。

这被捆绑工具用于 ESM(ECMAScript 模块)检测。 Rollup documentation 说得很好:

If your package.json file also has a module field, ES6-aware tools like Rollup and webpack 2 will import the ES6 module version directly.

This article on Rollup 1.0 换句话说:

The main field makes sure that Node users using require will be served the UMD version. The module field is not an official npm feature but a common convention among bundlers to designate how to import an ESM version of our library.

pkg.module 的进一步讨论在 Rollup Github Wiki and the webpack Docs