Javascript 什么是“模块”package.json 字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42708484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the "module" package.json field for?
提问by ryanve
I've seen some npm packages (vue for example) have a pkg.modulefield in their package.json. Is "module"an official npm property or is this a convention of some kind? Is there documentation for this somewhere? What's it for? esnext path? Unbundled module path?
我见过一些 npm 包(例如 vue)pkg.module在它们的 package.json 中有一个字段。是"module"官方的 npm 属性还是某种约定?某处有这方面的文档吗?这是为了什么?esnext路径?未捆绑的模块路径?
回答by Josh Habdas
Is
"module"an official npm property or is this a convention of some kind?
是
"module"官方的 npm 属性还是某种约定?
It's a proposal, but likely to be supported due to de facto usage.
这是一个提案,但由于实际使用可能会得到支持。
Is there documentation for this somewhere?
某处有这方面的文档吗?
There is, in fact, and it can be found right hereand later removed here.
What's it for?
这是为了什么?
ES6 Module Interoperability in Node. Additional discussion can be found hereand here. And here's a blog postfrom Rich Harris talking more about it.
Node.js 中的 ES6 模块互操作性 可以在此处和此处找到其他讨论。这是Rich Harris的一篇博客文章,详细介绍了它。
回答by Hyman Steam
This is used by bundler tools for ESM (ECMAScript Module) detection. The Rollup documentationsays it pretty well:
这被捆绑工具用于 ESM(ECMAScript 模块)检测。该汇总的文件说,这非常好:
If your
package.jsonfile also has amodulefield, ES6-aware tools like Rollupand webpack 2will import the ES6 module versiondirectly.
如果你的
package.json文件也有一个module字段,像Rollup和webpack 2这样的 ES6-aware 工具会直接导入 ES6 模块版本。
This article on Rollup 1.0says it another way:
这篇关于 Rollup 1.0 的文章说的是另一种方式:
The
mainfield makes sure that Node users usingrequirewill be served the UMD version. Themodulefield is not an official npm feature but a common convention among bundlers to designate how to import an ESM version of our library.
该
main字段确保使用的 Node 用户require将获得 UMD 版本。该module字段不是官方的 npm 功能,而是打包程序之间的通用约定,用于指定如何导入我们库的 ESM 版本。
Further discussion of pkg.moduleis on the Rollup Github Wikiand the webpack Docs.
进一步讨论pkg.module在Rollup Github Wiki和webpack Docs 上。

