查看正在运行的 npm 版本

¥See what version of npm you're running

npm -v

*nix 上升级(OSX、Linux 等)

¥Upgrading on *nix (OSX, Linux, etc.)

(你可能需要在这些命令前加上 sudo,特别是在 Linux 或 OS X 上,如果你使用默认安装程序安装 Node。)

¥*(You may need to prefix these commands with sudo, especially on Linux, or OS X 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)。

    ¥Option 1: edit your Windows installation's PATH to put %appdata%\npm before %ProgramFiles%\nodejs. Remember that you'll need to restart cmd.exe (and potentially restart Windows) when you make changes to PATH or how npm is installed.

  • 选项 2:删除两个

    ¥Option 2: remove both of

    • %ProgramFiles%\nodejs\npm

    • %ProgramFiles%\nodejs\npm.cmd

  • 选项 3:导航到 %ProgramFiles%\nodejs\node_modules\npm 并将 npmrc 文件复制到另一个文件夹或桌面。然后以管理员身份打开 cmd.exe,运行以下命令:

    ¥Option 3: Navigate to %ProgramFiles%\nodejs\node_modules\npm and copy the npmrcfile to another folder or the desktop. Then open cmd.exe as an administrator and run the following commands:

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

    ¥Option 1 or 2

    • 进入 %ProgramFiles%\nodejs\node_modules\npm 并将名为 npmrc 的文件复制到新的 npm 文件夹中,该文件夹应为 %appdata%\npm\node_modules\npm。这将告诉新的 npm 全局安装的包在哪里。

      ¥Go into %ProgramFiles%\nodejs\node_modules\npm and copy the file named npmrc in the new npm folder, which should be %appdata%\npm\node_modules\npm. This will tell the new npm where the global installed packages are.

  • 选项 3

    ¥Option 3

    • 将 npmrc 文件复制回 %ProgramFiles%\nodejs\node_modules\npm

      ¥Copy the npmrc file back into %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,你可以运行以下命令进行更正:

¥If it isn't set to <X>:\Users\<user>\AppData\Roaming\npm, you can run the below command to correct it:

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%)。

¥...as well as copying %APPDATA%\npm to %LOCALAPPDATA%\npm (and updating your %PATH%, of course).

每个在 npm 上工作的人都知道这个过程是复杂而令人担忧的,我们正在努力让它变得更简单。敬请关注。

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

npm 中文网 - 粤ICP备13048890号