且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在package.json中使用'main'参数?

更新时间:2023-12-03 23:40:34

来自 npm文档:

主要字段是模块ID,是您的主要入口点 程序.也就是说,如果您的软件包名为foo,并且有用户安装 它,然后确实require("foo"),然后您的主模块的导出 对象将被返回.

The main field is a module ID that is the primary entry point to your program. That is, if your package is named foo, and a user installs it, and then does require("foo"), then your main module's exports object will be returned.

这应该是相对于包根目录的模块ID 文件夹.

This should be a module ID relative to the root of your package folder.

对于大多数模块,拥有主脚本和 通常不会太多.

For most modules, it makes the most sense to have a main script and often not much else.

简而言之:

  1. 如果包的入口点与其根文件夹中的index.js不同,则仅在package.json中需要一个main参数.例如,人们经常将入口指向lib/index.jslib/<packagename>.js,在这种情况下,相应的脚本必须在package.json中描述为main.
  2. 您不能将两个脚本作为main,只是因为必须明确定义入口点require('yourpackagename').
  1. You only need a main parameter in your package.json if the entry point to your package differs from index.js in its root folder. For example, people often put the entry point to lib/index.js or lib/<packagename>.js, in this case the corresponding script must be described as main in package.json.
  2. You can't have two scripts as main, simply because the entry point require('yourpackagename') must be defined unambiguously.