在 Node.js 和 npm
生态系统中,开源项目的持续发展和维护常常依赖于贡献者的支持和资助。为了让开发者更容易了解他们依赖的项目哪些有资金支持选项,npm 在6.13.0
版本起引入了 npm fund
命令并默认在npm install
安装依赖时触发。本文将详细介绍 npm fund
的作用、运行机制、触发时机、如何避免触发以及相关的副作用和改进建议。
在日常npm install
安装依赖的过程中,我们可能都忽略了 command 最后输出的一些信息,比如本文相关的 funding 信息,如:
3 packages are looking for funding.
Run "npm fund" to find out more.
npm fund
命令是在 npm 6.13.0
版本中首次引入的,旨在帮助开发者识别其项目依赖中可以资助的开源包。运行该命令时,npm 会列出所有包含资助选项的包及其相关链接,便于开发者快速访问这些页面并提供支持。
在 2019 年 8 月份时,Standard JS 在其开源项目中内置广告的事件引发热议,这些广告通过一个名为 Funding
的 npm
软件包展示在终端,该软件包包含在 Standard
的代码库中。之后 npm
公司宣布将禁止此类终端广告行为。
此事件后,npm
公司表示,它打算在年底前为开源开发人员开发一个众筹平台,于是乎在npm 6.13.0
版本上提供了相应支持,这就是npm fund
命令的主要由来。
npm fund
会扫描项目中的 node_modules
目录,查找每个包的 package.json
文件中是否包含 funding
字段。它会将有资助选项的包及其资助链接列出。npm
提高了对开源项目资助的透明度,鼓励开发者参与到开源项目的资助中,帮助维护者获得资金支持。基本用法非常简单,只需在项目根目录中运行:
npm fund
命令将输出形如:
xx packages are looking for funding
run `npm fund` for details
再运行 npm fund,就会显示类似如下的详细信息:
package-name https://example.com/donate
如:
├─┬ https://opencollective.com/typescript-eslint
│ │ └── @typescript-eslint/[email protected], @typescript-eslint/[email protected], @typescript-eslint/[email protected], @typescript-eslint/[email protected], @typescript-eslint/[email protected], @typescript-eslint/[email protected], @typescript-eslint/[email protected]
│ └─┬ https://opencollective.com/eslint
│ │ └── [email protected]
│ ├── https://github.com/sponsors/epoberezkin
│ │ └── [email protected]
│ ├── https://github.com/sponsors/sindresorhus
│ │ └── [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]
│ ├── https://github.com/sponsors/isaacs
│ │ └── [email protected]
│ ├─┬ https://github.com/chalk/chalk?sponsor=1
│ │ │ └── [email protected]
│ │ └── https://github.com/chalk/ansi-styles?sponsor=1
│ │ └── [email protected]
│ └── https://github.com/sponsors/ljharb
│ └── [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]
├── https://github.com/sponsors/RubenVerborgh
│ └── [email protected]
└── https://ko-fi.com/tunnckoCore/commissions
└── [email protected]
npm fund
会读取 node_modules
中每个依赖包的 package.json
文件,寻找 funding
字段。如果找到了该字段,它会提取并显示相关的资助信息。
funding
字段可以是 URL 字符串或更复杂的对象,指向资助页面。例如:{
"funding": "https://example.com/donate"
}
或者{
"funding": {
"type": "individual",
"url": "https://example.com/donate"
}
}
npm install
提示:在安装项目依赖时,如果项目中存在可以资助的包,npm
会显示类似“xx packages are looking for funding”
的提示,提醒开发者可以运行 npm fund
查看详细信息。(这也是我们日常主要触发的时机)npm fund
命令,以查看当前项目中支持资助的所有包和资助链接。在某些情况下,开发者或企业可能希望在 npm install
过程中避免看到这些资助提示。以下是两种实现方式:
--no-fund
参数直接在运行 npm install
时添加 --no-fund
参数:
npm install --no-fund
.npmrc
配置文件在 .npmrc
文件中加入以下配置来永久禁用资助提示:
fund=false
此配置可以放在项目的根目录下(项目下的.npmrc
文件),仅作用于当前项目;也可以放在用户主目录(~/.npmrc
文件),作用于全局。
*可以通过
npm config ls -l
查看当前项目的npm
配置,默认情况下fund
配置会被设置为true
优点:
npm fund
提示可以减少 npm install
的输出信息,使终端显示更加清晰。缺点:
源码文件:https://github.com/npm/cli/blob/latest/lib/commands/fund.js
以目前(2024-11)的源码内容来看,其源码机制概括来说是先使用 npm
的内部模块库函数来遍历 node_modules
目录,读取 package.json
并检查是否有 funding
字段,最后将所有符合条件的包信息格式化输出到终端。
代码流程总结:
Arborist
加载项目的依赖树。libnpmfund
的 readTree
方法提取资助信息。archy
进行可读格式的输出。openFundingUrl
会尝试在浏览器中打开该包的资助链接。npm fund
是 npm
引入的一个有用的命令,帮助开发者支持开源项目并维持开源生态的可持续发展。虽然在某些情况下禁用它有其合理性,但在默认情况下保留该提示可以提高团队对开源项目支持的意识。根据项目和团队的实际需求,开发者可以灵活选择是否禁用 npm fund
提示。
npm install
?