查看正在运行的 npm 版本

🌐 See what version of npm you're running

npm -v

*nix 上升级(macOS、Linux 等)

🌐 Upgrading on *nix (macOS, Linux, etc.)

(你可能需要在这些命令前加上 sudo 前缀,尤其是在 Linux 上,或者在 macOS 上如果你是通过默认安装程序安装 Node 的话。)

🌐 (You may need to prefix these commands with sudo, especially on Linux, or macOS if you installed Node using its default installer.)

你可以使用以下命令升级到最新版本的 npm:

🌐 You can upgrade to the latest version of npm using:

npm install -g npm@latest

在 Windows 上升级

🌐 Upgrading on Windows

默认情况下,npm 安装在 node 旁边

🌐 By default, npm is installed alongside node in

C:\Program Files (x86)\nodejs

npm 的全局安装包(可能包括 npm 本身)单独存储在用户特定的目录中(当前是

🌐 npm's globally installed packages (including, potentially, npm itself) are stored separately in a user-specific directory (which is currently

C:\Users\<username>\AppData\Roaming\npm).

因为安装程序放

🌐 Because the installer puts

C:\Program Files (x86)\nodejs

before

C:\Users\<username>\AppData\Roaming\npm

在你的 PATH 上,它将始终使用随 node 安装的 npm 版本,而不是你使用 npm -g install npm@<version> 安装的 npm 版本。

🌐 on your PATH, it will always use the version of npm installed with node instead of the version of npm you installed using npm -g install npm@<version>.

要解决这个问题,你可以采取以下一种方式:

🌐 To get around this, you can do one of the following:

  • 选项 1:编辑你的 Windows 安装的 PATH,将 %appdata%\npm 放在 %ProgramFiles%\nodejs 之前。请记住,当你更改 PATH 或 npm 的安装方式时,你需要重启 cmd.exe(并可能需要重启 Windows)。
  • 选项 2:同时移除
    • %ProgramFiles%\nodejs\npm
    • %ProgramFiles%\nodejs\npm.cmd
  • 选项3:导航到 %ProgramFiles%\nodejs\node_modules\npm 并将 npmrc 文件复制到另一个文件夹或桌面。然后以管理员身份打开 cmd.exe 并运行以下命令:
cd %ProgramFiles%\nodejs
npm install npm@latest

如果你使用 node.js 安装程序安装了 npm,则在执行上述步骤之一后,请执行以下操作。

🌐 If you installed npm with the node.js installer, after doing one of the previous steps, do the following.

  • 选项 1 或 2
    • 进入 %ProgramFiles%\nodejs\node_modules\npm 并将名为 npmrc 的文件复制到新的 npm 文件夹中,该文件夹应为 %appdata%\npm\node_modules\npm。这将告诉新的 npm 全局安装的包的位置。
  • 选项 3
    • 将 npmrc 文件复制回 %ProgramFiles%\nodejs\node_modules\npm

(如果你使用的是 Windows 7 并且没有 %appdata%\npm 目录,请参见下面的要点。)

🌐 (See also the point below if you're running Windows 7 and don't have the directory %appdata%\npm.)

Windows 内置配置简要说明

🌐 A brief note on the built-in Windows configuration

Node 安装程序会直接在 npm 文件夹中安装一段特定于 Windows 的配置,该配置告诉 npm 全局包的安装位置。当使用 npm 安装自身时,它应该将这段特殊的 builtin 配置复制到新的安装中。在某些版本的 npm 中存在一个错误,使得此功能无法正常工作,因此你可能需要手动进行修复。运行以下命令可以查看 npm 将全局包安装到哪里,以验证是否正确。

🌐 The Node installer installs, directly into the npm folder, a special piece of Windows-specific configuration that tells npm where to install global packages. When npm is used to install itself, it is supposed to copy this special builtin configuration into the new install. There was a bug in some versions of npm that kept this from working, so you may need to go in and fix that up by hand. Run the following command to see where npm will install global packages to verify it is correct.

npm config get prefix -g

如果未设置为 <X>:\Users\<user>\AppData\Roaming\npm,你可以运行以下命令进行更正:

npm config set prefix %APPDATA%\npm -g

顺便说一句,如果你不希望将包安装到你的漫游配置文件中(因为你的共享网络上有配额,或者它使从域登录或退出变得缓慢),你可以将其放入本地应用数据中:

🌐 Incidentally, if you would prefer that packages not be installed to your roaming profile (because you have a quota on your shared network, or it makes logging in or out from a domain sluggish), you can put it in your local app data instead:

npm config set prefix %LOCALAPPDATA%\npm -g

...以及将 %APPDATA%\npm 复制到 %LOCALAPPDATA%\npm(当然还要更新你的 %PATH%)。

所有在 npm 工作的人都知道这个过程既复杂又充满风险,我们正在努力使它更简单。敬请关注。

🌐 Everyone who works on npm knows that this process is complicated and fraught, and we're working on making it simpler. Stay tuned.