npm-doctor

检查你的 npm 环境

选择命令行版本:

概要

🌐 Synopsis

npm doctor [ping] [registry] [versions] [environment] [permissions] [cache]

注意:此命令不支持工作区。

🌐 Note: This command is unaware of workspaces.

描述

🌐 Description

npm doctor 会运行一系列检查,以确保你的 npm 安装具备管理 JavaScript 包所需的条件。npm 大多是一个独立的工具,但它确实有一些必须满足的基本要求:

  • Node.js 和 git 必须可由 npm 执行。
  • 主要的 npm 注册表 registry.npmjs.com,或使用该注册表 API 的其他服务,都是可用的。
  • npm 使用的目录 node_modules(本地和全局)存在,并且当前用户可以写入。
  • npm 缓存存在,并且其中的包 tarball 没有损坏。

如果这些都无法正常工作,npm 可能无法正常运行。许多问题通常归因于 npm 代码库之外的因素,因此 npm doctor 用于确认 npm 安装处于良好状态。

🌐 Without all of these working properly, npm may not work properly. Many issues are often attributable to things that are outside npm's code base, so npm doctor confirms that the npm installation is in a good state.

此外,除此之外,由于使用旧版本的 npm,也有很多问题报告。由于 npm 不断改进,运行 npm@latest 比使用旧版本要好。

🌐 Also, in addition to this, there are also very many issue reports due to using old versions of npm. Since npm is constantly improving, running npm@latest is better than an old version.

npm doctor 会验证你环境中的以下项目,如果有任何建议的更改,它将显示出来。默认情况下,npm 会运行所有这些检查。你可以通过将它们作为额外参数指定来限制运行的检查项。

npm ping

默认情况下,npm 从主 npm 注册表 registry.npmjs.org 安装。npm doctor 会访问注册表内的一个特殊 ping 端点。也可以使用 npm ping 来检查。如果此检查失败,你可能正在使用需要配置的代理,或者需要联系你的 IT 人员以通过 HTTPS 访问 registry.npmjs.org

🌐 By default, npm installs from the primary npm registry, registry.npmjs.org. npm doctor hits a special ping endpoint within the registry. This can also be checked with npm ping. If this check fails, you may be using a proxy that needs to be configured, or may need to talk to your IT staff to get access over HTTPS to registry.npmjs.org.

此检查会针对你配置的注册表进行(你可以通过运行 npm config get registry 查看具体是哪个注册表),如果你使用的私有注册表不支持主注册表的 /whoami 端点,则此检查可能会失败。

🌐 This check is done against whichever registry you've configured (you can see what that is by running npm config get registry), and if you're using a private registry that doesn't support the /whoami endpoint supported by the primary registry, this check may fail.

npm -v

虽然 Node.js 可能会打包特定版本的 npm,但 CLI 团队的政策是,我们建议所有用户在可能的情况下运行 npm@latest。由于 CLI 由一个小型贡献者团队维护,因此只能支持单一开发线路,因此 npm 的长期支持版本通常只会收到关键的安全和回归修复。该团队认为,最新经过测试的 npm 版本几乎总是最有可能功能最全且无缺陷的版本。

🌐 While Node.js may come bundled with a particular version of npm, it's the policy of the CLI team that we recommend all users run npm@latest if they can. As the CLI is maintained by a small team of contributors, there are only resources for a single line of development, so npm's own long-term support releases typically only receive critical security and regression fixes. The team believes that the latest tested version of npm is almost always likely to be the most functional and defect-free version of npm.

node -v

对于大多数用户来说,在大多数情况下,最适合的 Node 版本是最新的长期支持(LTS)版本。那些希望使用最新 ECMAScript 功能或 Node 标准库前沿更改的用户可能会运行较新的版本,而有些用户可能由于企业变更控制政策需要运行较旧的 Node 版本。这没问题!但总体而言,npm 团队建议大多数用户使用 Node.js LTS 版本。

🌐 For most users, in most circumstances, the best version of Node will be the latest long-term support (LTS) release. Those of you who want access to new ECMAscript features or bleeding-edge changes to Node's standard library may be running a newer version, and some may be required to run an older version of Node because of enterprise change control policies. That's OK! But in general, the npm team recommends that most users run Node.js LTS.

npm config get registry

你可能正在为你的项目或公司从私有包注册表安装。这很好!其他人可能正在按照教程或 StackOverflow 问题来尝试解决你可能遇到的问题。有时,这可能需要更改你指向的注册表。npm doctor 的这一部分只是让你以及可能正在帮助你提供支持的人知道,你并没有使用默认注册表。

🌐 You may be installing from private package registries for your project or company. That's great! Others may be following tutorials or StackOverflow questions in an effort to troubleshoot problems you may be having. Sometimes, this may entail changing the registry you're pointing at. This part of npm doctor just lets you, and maybe whoever's helping you with support, know that you're not using the default registry.

which git

虽然在 README 中有记载,但可能不明显的是,npm 执行许多操作需要安装 Git。此外,在某些情况下——尤其是在 Windows 上——你可能以一种 npm 无法通过 PATH 访问的方式设置了 Git。此检查用于确保 Git 可用。

🌐 While it's documented in the README, it may not be obvious that npm needs Git installed to do many of the things that it does. Also, in some cases – especially on Windows – you may have Git set up in such a way that it's not accessible via your PATH so that npm can find it. This check ensures that Git is available.

权限检查

🌐 Permissions checks

  • 你的缓存必须可由运行 npm 的用户读写。
  • 全局包二进制文件必须可由运行 npm 的用户写入。
  • 如果你在带有项目目录的情况下运行 npm doctor,本地的 node_modules 路径必须对运行 npm 的用户可读可写。

验证缓存包的校验和

🌐 Validate the checksums of cached packages

当 npm 包发布时,发布过程会生成一个校验和,npm 在安装时使用该校验和来验证包在传输过程中是否损坏。npm doctor 使用这些校验和来验证本地缓存中的包 tarball(你可以使用 npm config get cache 查看缓存所在位置)。如果缓存中存在损坏的包,你可能需要运行 npm cache clean -f 并重置缓存。

🌐 When an npm package is published, the publishing process generates a checksum that npm uses at install time to verify that the package didn't get corrupted in transit. npm doctor uses these checksums to validate the package tarballs in your local cache (you can see where that cache is located with npm config get cache). In the event that there are corrupt packages in your cache, you should probably run npm cache clean -f and reset the cache.

配置

🌐 Configuration

registry

  • 默认: "https://registry.npmjs.org/"
  • 类型:网址

npm 注册表的基本 URL。

🌐 The base URL of the npm registry.

也可以看看

🌐 See Also