# npm

**npm** 是 [JavaScript](https://www.edgepedia.org/zh/javascript) 生态系统的包管理器：一个命令行工具，搭配 npm 注册表——一个面向 Node.js、前端 Web 应用、移动应用等的开源包公共集合。<sup>[1](https://nodejs.org/learn/getting-started/an-introduction-to-the-npm-package-manager)</sup><sup> • </sup><sup>[2](https://www.npmjs.com/about)</sup> 它最初是为了下载和管理 Node.js 包依赖而诞生的，后来也成为前端 JavaScript 中使用的工具。<sup>[1](https://nodejs.org/learn/getting-started/an-introduction-to-the-npm-package-manager)</sup> 2022 年 9 月，其注册表收录了超过 210 万个包，被称为地球上最大的单一语言代码仓库。<sup>[1](https://nodejs.org/learn/getting-started/an-introduction-to-the-npm-package-manager)</sup>

| 关键事实 | 详情 |
|---|---|
| 创建 | 2009 年由 Isaac Schlueter 创建，紧随 Node.js 首次公开发布之后<sup>[3](https://www.wired.com/2015/04/npm-funding/)</sup> |
| 所有权 | 2020 年 3 月被 GitHub（微软）收购；公共注册表仍然免费<sup>[4](https://techcrunch.com/2020/03/16/github-nabs-javascript-packaging-vendor-npm/)</sup> |
| 注册表规模 | 超过 300 万个包，每周服务数百亿次下载<sup>[5](https://arxiv.org/html/2308.12545)</sup> |
| 下载量 | 截至 2023 年底，每月包下载量超过 1840 亿次<sup>[6](https://socket.dev/blog/2023-npm-retrospective)</sup> |
| 依赖模型 | package.json 中的 semver 范围，依据锁文件解析；默认采用提升（扁平）式 node_modules 布局<sup>[7](https://docs.npmjs.com/cli/v11/commands/npm-install/)</sup> |
| 传递依赖 | 平均每个包 683 个，而 PyPI 为 19 个，RubyGems 为 68 个<sup>[8](https://blog.kevinroleke.com/npm)</sup> |
| 重大安全事件 | 2025 年 9 月，Shai-Hulud 自复制蠕虫攻陷了 500 多个包<sup>[9](https://www.cisa.gov/news-events/alerts/2025/09/23/widespread-supply-chain-compromise-impacting-npm-ecosystem)</sup> |

## 历史与治理

2009 年，Isaac Schlueter 在 Node 首次公开发布后不久创建了 npm。很快，他在自己的 Node 项目中开始依赖他人编写的模块，并寻求一种将它们组织起来的方法。<sup>[3](https://www.wired.com/2015/04/npm-funding/)</sup> 到 2010 年初，一个可用的版本已经存在；到 2011 年第一届 NodeConf 时，注册表托管了约 2,000 个包，到第二届时约有 12,000 个。<sup>[10](https://thenextweb.com/news/microsoft-finally-s-open-source-a-chat-with-npms-founder)</sup>

**开放发布作为一种设计选择。** Schlueter 将 npm 设计成任何人都可以向他的服务器添加包，而无需事先获得他的批准。这帮助它迅速成长，并很快成为安装 Node 软件的标准方式。<sup>[3](https://www.wired.com/2015/04/npm-funding/)</sup> 同样的开放发布模式也与注册表上的垃圾信息和恶意发布有关。<sup>[3](https://www.wired.com/2015/04/npm-funding/)</sup><sup> • </sup><sup>[6](https://socket.dev/blog/2023-npm-retrospective)</sup>

npm Inc. 成立于 2014 年，目的是在服务器宕机以及至少一次重大安全问题之后资助全职工程工作；企业客户向 npm 付费，以获得私有托管和符合安全审查要求的开源包镜像。<sup>[3](https://www.wired.com/2015/04/npm-funding/)</sup> 根据 PitchBook 的数据，该公司在以 4800 万美元投后估值计算的情况下，已筹集近 1900 万美元。<sup>[4](https://techcrunch.com/2020/03/16/github-nabs-javascript-packaging-vendor-npm/)</sup> 2020 年 3 月，微软旗下的开发者代码仓库 GitHub 以未披露的金额收购了 npm，当时 npm 拥有超过 130 万个包，每月下载量达 750 亿次；GitHub 承诺公共注册表将永远可用、永远免费。<sup>[4](https://techcrunch.com/2020/03/16/github-nabs-javascript-packaging-vendor-npm/)</sup>

## 工作原理

项目在 **package.json** 中声明其依赖，这是一个将包名映射到版本范围的对象；依赖也可以用 tarball 或 git URL 来标识。<sup>[11](https://docs.npmjs.com/cli/v11/configuring-npm/package-json/)</sup> `dependencies` 字段列出生产环境中与应用捆绑的包，而 `devDependencies` 包含测试库等开发工具。<sup>[1](https://nodejs.org/learn/getting-started/an-introduction-to-the-npm-package-manager)</sup> `peerDependencies` 声明与宿主包的兼容性：在 npm 版本 3 到 6 中，它们不会被自动安装，如果发现无效版本则会发出警告，但从 npm v7 起，它们默认会被安装，除非标记为可选。<sup>[11](https://docs.npmjs.com/cli/v11/configuring-npm/package-json/)</sup> 版本字符串遵循语义化版本（semver）标准。<sup>[1](https://nodejs.org/learn/getting-started/an-introduction-to-the-npm-package-manager)</sup> 自 npm 5 起，`npm install` 无需 `--save` 标志即可将包添加到 package.json 的 dependencies 中。<sup>[1](https://nodejs.org/learn/getting-started/an-introduction-to-the-npm-package-manager)</sup>

**锁文件与可复现性。** 如果项目有锁文件，安装将由它驱动，优先级顺序为 npm-shrinkwrap.json、package-lock.json、yarn.lock。<sup>[7](https://docs.npmjs.com/cli/v11/commands/npm-install/)</sup> 如果锁文件中记录的版本满足 package.json 中的约束，npm 会精确安装这些 package-lock.json 版本，以便在不同环境中以相同方式复现构建；否则 package.json 优先，锁文件会被刷新。<sup>[7](https://docs.npmjs.com/cli/v11/commands/npm-install/)</sup> `npm ci` 命令在安装依赖时保证 package.json 保持不变，并确保两个文件完全同步。<sup>[7](https://docs.npmjs.com/cli/v11/commands/npm-install/)</sup> 一项针对各包管理器锁文件设计的调查发现，npm、pnpm、Poetry、Gradle 和 Go 默认遵循锁文件，只包含锁定的版本，而 Cargo 和 Pipenv 除非使用 `--locked` 或 `--frozen` 等标志，否则无论锁定版本如何都会解析依赖；除 Gradle 外，所有受调查的管理器默认都会生成锁文件。<sup>[12](https://arxiv.org/abs/2505.04834)</sup>

**node_modules 目录树。** npm 对 node_modules 目录支持提升（默认）、嵌套、浅层和链接安装策略。<sup>[7](https://docs.npmjs.com/cli/v11/commands/npm-install/)</sup> 默认的提升布局将共享依赖提升到顶层，这扁平化了目录树，但也让代码可以导入从未声明为依赖的包，即“幽灵依赖”问题。<sup>[13](https://v6.yarnpkg.com/getting-started/comparisons.html)</sup> 图中深处的冲突 peer 依赖默认使用最近的非 peer 依赖规范来解析；启用 `strict-peer-deps` 且未设置 `--legacy-peer-deps` 时，任何冲突的 peerDependencies 都会被视为安装失败。<sup>[7](https://docs.npmjs.com/cli/v11/commands/npm-install/)</sup> 强制指定特定版本的 overrides 只在项目的根 package.json 中考虑，不在已安装的依赖或工作区中考虑。<sup>[11](https://docs.npmjs.com/cli/v11/configuring-npm/package-json/)</sup>

## npm 注册表

注册表是一个面向 Node.js、前端 Web 应用、移动应用、机器人等的开源包公共集合。<sup>[2](https://www.npmjs.com/about)</sup> 任何人都可以发布，无需事先批准。<sup>[3](https://www.wired.com/2015/04/npm-funding/)</sup> 在 package.json 中设置 `"private": true` 会使 npm 拒绝发布该包。<sup>[11](https://docs.npmjs.com/cli/v11/configuring-npm/package-json/)</sup>

版本删除是一个可度量的可复现性问题：2022 年 7 月 12 日至 2023 年 5 月 10 日期间，有 335,325 个包版本从 npm 中被删除，这种损失对可复现性和恶意软件研究都是问题。<sup>[5](https://arxiv.org/html/2308.12545)</sup> 弃用是更温和的替代方案；2023 年的一份注册表概览统计了 82,747 个已取消发布的包和 72,754 个已弃用包。<sup>[14](https://blog.sandworm.dev/state-of-npm-2023-the-overview)</sup>

**谁付费。** 公共注册表是免费的；npm Inc. 最初的收入来自注册表中的私有包以及 2015 年底推出的企业本地部署注册表。<sup>[10](https://thenextweb.com/news/microsoft-finally-s-open-source-a-chat-with-npms-founder)</sup> 自 2020 年起，注册表由 GitHub 运营，后者承诺保持公共使用免费。<sup>[4](https://techcrunch.com/2020/03/16/github-nabs-javascript-packaging-vendor-npm/)</sup>

## 数据一览

注册表规模取决于如何计数以及何时计数。根据 npm 自己的数据，截至 2023 年底，注册表托管了超过 250 万个活跃包，月下载量超过 1840 亿次。<sup>[6](https://socket.dev/blog/2023-npm-retrospective)</sup> 另一项独立的注册表普查在 2023 年统计了总计 3,342,873 个包和 34,335,328 个版本，平均每个包 10.27 个版本，总版本大小为 24.17 TB，平均包大小为 416 KB。<sup>[14](https://blog.sandworm.dev/state-of-npm-2023-the-overview)</sup>

**跨注册表比较。** 截至 2025 年，各注册表的包数量为：PyPI 664,396 个，[Maven Central](https://www.edgepedia.org/zh/maven-central) 17,397,008 个，RubyGems 186,436 个，crates.io 191,888 个，Packagist 428,800 个，CPAN 222,283 个，CTAN 6,795 个。<sup>[8](https://blog.kevinroleke.com/npm)</sup> 按原始数量计算，Maven Central 更大，但 npm 的依赖密度独具特色：npm 包平均引入 683 个传递依赖，而 PyPI 为 19 个，RubyGems 为 68 个，PHP 为 70 个。<sup>[8](https://blog.kevinroleke.com/npm)</sup> 漏洞传播范围也相应广泛：在研究涉及的生态系统中，33% 和 40% 的外部 GitHub 项目暴露于来自直接或间接依赖的漏洞。<sup>[15](https://link.springer.com/article/10.1007/s10664-022-10154-1)</sup> 发布量巨大：2023 年向注册表发布了 10,518,566 个包版本，同时有 1,241,583 个版本被取消发布。<sup>[6](https://socket.dev/blog/2023-npm-retrospective)</sup>

## 与其他包管理器的比较

**Yarn 和 pnpm** 以不同方式解决幽灵依赖问题。Yarn 的默认安装策略 Plug'n'Play（PnP）通过 Node.js 加载器强制执行声明的依赖树，消除幽灵依赖；Yarn 还支持 pnpm 风格的符号链接和传统的 node_modules 链接器，其 PnP 模式避免写入 node_modules 目录树，使典型安装时间降至几秒。<sup>[13](https://v6.yarnpkg.com/getting-started/comparisons.html)</sup> pnpm 使用符号链接和内容寻址存储来构建嵌套的 node_modules 目录树，无需自定义加载器即可提供强大的幽灵依赖防护，尽管其布局无法表示所有依赖树，特别是涉及 peer 依赖的依赖树。<sup>[13](https://v6.yarnpkg.com/getting-started/comparisons.html)</sup>

**安装速度。** 基准测试一致显示 npm 最慢，但测得的差距因场景和基准运行而异。在 pnpm 发布的基准测试中，pnpm 12 的干净安装耗时 4.44 秒，而 npm v12.2.0 为 45.7 秒；“全热”场景下 pnpm 运行 17 毫秒，npm 为 1.04 秒，相差 61 倍；安装均通过模拟的 50 毫秒链路经过同一注册表。<sup>[16](https://pnpm.io/benchmarks)</sup> 配套的基准测试仓库测得热安装（有缓存、锁文件、现有 node_modules）为 npm 5.4 秒、pnpm 1.3 秒、Yarn 707 毫秒，完全冷安装为 npm 51 秒、pnpm 14.4 秒、Yarn 39.1 秒、Yarn PnP 29.1 秒。<sup>[17](https://github.com/pnpm/benchmarks-of-javascript-package-managers/blob/master/README.md)</sup> 两个由 pnpm 运行的基准测试给出了不同的 npm 干净安装时间——45.7 秒和 51 秒——反映的是不同的场景定义，而非一个确定的数字。

## 安全与供应链风险

注册表的开放性体现在其滥用统计数据中。2023 年，Socket 识别出超过五千个 npm 包为恶意软件，随后被注册表下架；2023 年 2 月，两天内发布了超过 15,000 个垃圾包；在 2023 年概览的 12 个月中，至少创建了 562,743 个垃圾包。<sup>[6](https://socket.dev/blog/2023-npm-retrospective)</sup><sup> • </sup><sup>[14](https://blog.sandworm.dev/state-of-npm-2023-the-overview)</sup> 另一主要攻击途径涉及账户被盗：2023 年 12 月 14 日，网络钓鱼者欺骗了一名 Ledger 前员工，并利用由此获得的该人 npmjs 账户访问权限，使一个有害的 Ledger Connect Kit 版本进入了 npm。<sup>[6](https://socket.dev/blog/2023-npm-retrospective)</sup> 2025 年 7 月，一场利用 npmjs.org 上缺失 SPF 记录的网络钓鱼活动攻陷了包括 eslint-config-prettier 和 is 在内的包，这些包的周下载量合计为 96,063,394 次。<sup>[8](https://blog.kevinroleke.com/npm)</sup>

**Shai-Hulud。** 2025 年 9 月，一个名为“Shai-Hulud”的自复制蠕虫攻陷了 npm 注册表上的 500 多个包。<sup>[9](https://www.cisa.gov/news-events/alerts/2025/09/23/widespread-supply-chain-compromise-impacting-npm-ecosystem)</sup> GitHub 于 2025 年 9 月 14 日获悉该事件，将其描述为一个自复制蠕虫，通过被盗的维护者账户进入 npm 生态系统，并通过向广泛使用的 JavaScript 包添加恶意安装后脚本对其进行攻击。<sup>[18](https://github.blog/security/supply-chain-security/our-plan-for-a-more-secure-npm-supply-chain/)</sup> 该恶意软件通过以被盗开发者身份向 npm 注册表进行认证、向其他包注入代码并发布被攻陷的版本来传播；它将收集到的凭据外传至攻击者控制的端点，并通过 GitHub 用户/仓库 API 上传到名为 `Shai-Hulud` 的公共 GitHub 仓库。<sup>[9](https://www.cisa.gov/news-events/alerts/2025/09/23/widespread-supply-chain-compromise-impacting-npm-ecosystem)</sup> GitHub 从注册表中移除了 500 多个被攻陷的包，并阻止了包含该恶意软件入侵指标的上传。<sup>[18](https://github.blog/security/supply-chain-security/our-plan-for-a-more-secure-npm-supply-chain/)</sup>

**防御措施。** CISA 建议将 npm 依赖版本固定到 2025 年 9 月 16 日之前生成的已知安全版本，立即轮换所有开发者凭据，并强制使用抗钓鱼的多因素认证。<sup>[9](https://www.cisa.gov/news-events/alerts/2025/09/23/widespread-supply-chain-compromise-impacting-npm-ecosystem)</sup> OWASP 建议强制执行锁文件，使 package.json 与锁文件之间的任何不一致都中止安装；在 CI 期间通过检查签名或来源来验证包；验证 SBOM；运行软件组成分析和静态分析；从固定的锁文件解析结果安装；以及限制、限定范围并轮换 CI 和发布者令牌。<sup>[19](https://cheatsheetseries.owasp.org/cheatsheets/NPM_Security_Cheat_Sheet.html)</sup>

**固定版本有帮助吗？** 可信来源意见不一。尽管上述事件响应建议将固定版本视为一种保障措施，但一项经过同行评审的 npm 生态系统反事实模拟显示，固定直接依赖不仅会增加易受攻击或过时依赖的维护负担，对于大型依赖图，甚至可能增加对恶意包更新的暴露风险，这是 npm 解析依赖方式的结果；该研究建议不要固定版本，而是采用协调固定方法，并对 npm 进行特定修改。<sup>[20](https://dl.acm.org/doi/abs/10.1145/3715728)</sup> 两种立场均在此报告；

## 近期发展

**npm 7 到 11。** 在 GitHub 旗下发布的 npm CLI v7.0.0 提供了工作区支持，可从单一顶层根包管理多个包；自动安装 peer 依赖；package-lock v2；以及 yarn.lock 支持；node_modules 目录树的检查和管理移至 Arborist 模块。<sup>[21](https://github.blog/news-insights/product-news/presenting-v7-0-0-of-the-npm-cli/)</sup> npm 11.0.0 移除了 `npm hook` 命令，使证明仅在 npm 10.6.0 或更高版本中验证，并支持 Node.js `^20.17.0 || >=22.9.0`。<sup>[22](https://github.com/npm/cli/releases/tag/v11.0.0)</sup>

**2025 年注册表安全变更。** 在 Shai-Hulud 之后，GitHub 宣布了发布变更：要求 2FA 的本地发布、有效期限制为七天的细粒度令牌、可信发布，以及[弃用](https://www.edgepedia.org/zh/deprecation)经典令牌和 TOTP 2FA，转而使用基于 FIDO 的 2FA。<sup>[18](https://github.blog/security/supply-chain-security/our-plan-for-a-more-secure-npm-supply-chain/)</sup> 可信发布将 API 令牌从构建流水线中移除，由 PyPI 于 2023 年 4 月首创，随后被 RubyGems（2023 年 12 月）、crates.io（2025 年 7 月）、npm（同样是 2025 年 7 月）和 NuGet（2025 年 9 月）采用。<sup>[18](https://github.blog/security/supply-chain-security/our-plan-for-a-more-secure-npm-supply-chain/)</sup> 维护者被建议立即切换到可信发布，对发布和写入强制执行 2FA，并使用 WebAuthn 而非基于时间的一次性密码。<sup>[23](https://www.bleepingcomputer.com/news/security/github-tightens-npm-security-with-mandatory-2fa-access-tokens/)</sup>

## References

1. [An introduction to the npm package manager, Node.js Learn](https://nodejs.org/learn/getting-started/an-introduction-to-the-npm-package-manager)
2. [npm About, npmjs.com](https://www.npmjs.com/about)
3. [Small Packages of Code Are the Biggest Thing in App-Making, WIRED (2015)](https://www.wired.com/2015/04/npm-funding/)
4. [GitHub nabs JavaScript packaging vendor npm, TechCrunch (2020)](https://techcrunch.com/2020/03/16/github-nabs-javascript-packaging-vendor-npm/)
5. [npm-follower: A Complete Dataset Tracking the NPM Ecosystem, MSR 2024](https://arxiv.org/html/2308.12545)
6. [npm in Review: A 2023 Retrospective on Growth, Security, and Quirky Facts, Socket](https://socket.dev/blog/2023-npm-retrospective)
7. [npm-install, npm Docs v11](https://docs.npmjs.com/cli/v11/commands/npm-install/)
8. [npm: How did we get here?, Kevin Roleke blog](https://blog.kevinroleke.com/npm)
9. [Widespread Supply Chain Compromise Impacting npm Ecosystem, CISA Alert (September 2025)](https://www.cisa.gov/news-events/alerts/2025/09/23/widespread-supply-chain-compromise-impacting-npm-ecosystem)
10. [Microsoft finally ❤'s open source: A chat with npm's founder, The Next Web](https://thenextweb.com/news/microsoft-finally-s-open-source-a-chat-with-npms-founder)
11. [package.json, npm Docs v11](https://docs.npmjs.com/cli/v11/configuring-npm/package-json/)
12. [The Design Space of Lockfiles Across Package Managers, arXiv:2505.04834](https://arxiv.org/abs/2505.04834)
13. [Comparisons, Yarn documentation](https://v6.yarnpkg.com/getting-started/comparisons.html)
14. [State Of Npm 2023: The Overview, Sandworm Blog](https://blog.sandworm.dev/state-of-npm-2023-the-overview)
15. [On the impact of security vulnerabilities in the npm and RubyGems dependency networks, Empirical Software Engineering](https://link.springer.com/article/10.1007/s10664-022-10154-1)
16. [Benchmarks of JavaScript Package Managers, pnpm](https://pnpm.io/benchmarks)
17. [benchmarks-of-javascript-package-managers README, pnpm GitHub](https://github.com/pnpm/benchmarks-of-javascript-package-managers/blob/master/README.md)
18. [Our plan for a more secure npm supply chain, The GitHub Blog](https://github.blog/security/supply-chain-security/our-plan-for-a-more-secure-npm-supply-chain/)
19. [NPM Security Cheat Sheet, OWASP](https://cheatsheetseries.owasp.org/cheatsheets/NPM_Security_Cheat_Sheet.html)
20. [Pinning Is Futile: You Need More Than Local Dependency Versioning to Defend against Supply Chain Attacks, Proceedings of the ACM on Software Engineering](https://dl.acm.org/doi/abs/10.1145/3715728)
21. [Presenting v7.0.0 of the npm CLI, The GitHub Blog](https://github.blog/news-insights/product-news/presenting-v7-0-0-of-the-npm-cli/)
22. [v11.0.0, npm/cli Release](https://github.com/npm/cli/releases/tag/v11.0.0)
23. [GitHub tightens npm security with mandatory 2FA, access tokens, BleepingComputer](https://www.bleepingcomputer.com/news/security/github-tightens-npm-security-with-mandatory-2fa-access-tokens/)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Build, deployment, and development tooling › Package managers

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/npm

English original: https://www.edgepedia.org/npm
