如何将 NodeJS 和 NPM 更新到下一个版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6237295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:01:43  来源:igfitidea点击:

How can I update NodeJS and NPM to the next versions?

node.jslinuxnpmnode-modulesnpm-update

提问by Dail

I just installed Node.jsand npm(for additional modules).

我刚刚安装了Node.jsnpm(用于附加模块)。

How can I update Node.js and the modules which I'm using to the latest versions?

如何将 Node.js 和我使用的模块更新到最新版本?

Can npmdo it, or do I have to remove and reinstall Node.js and npm to get the next versions?

可以npm这样做,还是必须删除并重新安装 Node.js 和 npm 才能获得下一个版本?

I followed thissteps in the npmsection.

我在本节中遵循了步骤npm

回答by James

See the docs for the updatecommand:

有关update命令,请参阅文档:

npm update [-g] [<pkg>...]

This command will update all the packages listed to the latest version (specified by the tag config), respecting semver.

此命令会将列出的所有软件包更新为最新版本(由标签配置指定),尊重 semver。

Additionally, see the documentation on Node.js and NPM installationand Upgrading NPM.

此外,请参阅有关Node.js 和 NPM 安装升级 NPM的文档。

The following original answer is from the old FAQ that no longer exists, but should work for Linux and Mac:

以下原始答案来自不再存在的旧常见问题解答,但应该适用于 Linux 和 Mac:

How do I update npm?

npm install -g npm

Please note that this command will remove your current version of npm. Make sure to use sudo npm install -g npmif on a Mac.

You can also update all outdated local packages by doing npm updatewithout any arguments, or global packages by doing npm update -g.

Occasionally, the version of npm will progress such that the current version cannot be properly installed with the version that you have installed already. (Consider, if there is ever a bug in the update command.) In those cases, you can do this:

curl https://www.npmjs.com/install.sh | sh

我如何更新 npm?

npm install -g npm

请注意,此命令将删除您当前版本的 npm。确保sudo npm install -g npm在 Mac 上使用if。

您还可以npm update不带任何参数执行更新所有过时的本地包,或执行npm update -g.

有时,npm 的版本会进步,以至于当前版本无法与您已经安装的版本一起正确安装。(考虑一下,如果 update 命令中存在错误。)在这些情况下,您可以这样做:

curl https://www.npmjs.com/install.sh | sh

To update Node.js itself, I recommend you use nvm, the Node Version Manager.

要更新 Node.js 本身,我建议您使用nvm,即节点版本管理器

回答by Anmol Saraf

I understand this question is for Linux machine but just in case anybody is looking for a Windows solution, just go to the Node.js site, click the downloadbutton on the homepage and execute the installer program.

我知道这个问题是针对 Linux 机器的,但以防万一有人正在寻找 Windows 解决方案,只需转到Node.js 站点,单击主页上的下载按钮并执行安装程序。

Thankfully it took care of everything and with a few clicks of 'Next' button I got the latest 0.8.15 Node.js version running on my Windows 7 machine.

值得庆幸的是,它处理了所有事情,只需点击几下“下一步”按钮,我就可以在我的 Windows 7 机器上运行最新的 0.8.15 Node.js 版本。

回答by nkh

I found this really neat way of updating node on David Walsh's blog, you can do it by installing n:

我在David Walsh 的博客上发现了这种更新节点的非常巧妙的方法,您可以通过安装n

sudo npm cache clean -f
sudo npm install -g n
sudo n stable

It will install the current stable version of node.

它将安装当前稳定版本的node.



EDIT:Please don't use n anymore. I recommend using nvm. You can simply install stable by following the commands below:

编辑:请不要再使用 n 了。我建议使用nvm. 您可以按照以下命令简单地安装稳定版:

nvm ls-remote
nvm install <version> 
nvm use <version>

回答by B T

  • To update nodeuse nvm(or nvmwfor windows).

  • To update npm, the npm update npm -gcommand didn't work for me (on windows). What did work was reinstalling npm according to the documentation: "You can download a zip file from https://npmjs.org/dist/, and unpack it in the same folder where node.exe lives." Make sure if you do this that you get rid of your previous installation first (though overwriting it will probably work ok...).

  • To update your modules, use the npm update command

  • 更新节点,请使用nvm(或nvmwfor windows)。

  • 更新 npm,该npm update npm -g命令对我不起作用(在 Windows 上)。根据文档重新安装 npm 的工作是什么:“您可以从https://npmjs.org/dist/下载一个 zip 文件,并将其解压到 node.exe 所在的同一文件夹中。”请确保您是否这样做您首先摆脱了以前的安装(尽管覆盖它可能会正常工作......)。

  • 更新您的模块,请使用npm update 命令

回答by Jason Lydon

I recently stumbled across this article: http://martineau.tv/blog/2013/12/more-efficient-grunt-workflows/and the author mentions $ npm-check-updates -u && npm installto update all dependencies.

我最近偶然发现了这篇文章:http: //martineau.tv/blog/2013/12/more-efficient-grunt-workflows/,作者提到$ npm-check-updates -u && npm install更新所有依赖项。

This is a little off the topic but I ended up here on a similar search so thought it was worth the share.

这有点偏离主题,但我最终在这里进行了类似的搜索,因此认为值得分享。

回答by Dan K.K.

As you may already know, npmis currently bundled with node.js. It means that if you have installed node.js, you've already installed npmas well.

您可能已经知道,npm目前与node.js捆绑在一起。这意味着如果你已经安装了node.js,那么你也已经安装了npm

Also, pay attention to the node.jsand npmrelease versions tablethat shows us approximate versions compatibility. Sometimes, versions discrepancy may cause incompatibility errors.

另外,请注意node.jsnpm发布版本表,它向我们展示了大致的版本兼容性。有时,版本差异可能会导致不兼容错误。

So, if you're a developer, it's kinda "best practice" to manage your development environment using one of the node.jsversion managers.

因此,如果您是一名开发人员,那么使用其中一个node.js版本管理器来管理您的开发环境是一种“最佳实践” 。

Here is a list and usage notes of some of the most popular:

以下是一些最受欢迎的列表和使用说明:

Homebrew (macOS)

自制软件(macOS)

If you're on macOS, you can use Homebrew.

如果您使用的是macOS,则可以使用Homebrew

Actually, it's not just a node.jsversion manager.

实际上,它不仅仅是一个node.js版本管理器。

To install Homebrewto your Mac:

要将Homebrew安装到您的 Mac:

$ ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

To install node.jsand npmusing Homebrew, run:

要使用 Homebrew安装node.jsnpm,请运行:

$ brew install node

Later, you will be able to update them using:

稍后,您将能够使用以下方法更新它们:

$ brew update && brew upgrade node

Also, you can switchbetween node.jsversions as well:

此外,您还可以在node.js版本之间切换

$ brew switch node 0.10.26

npmwill be upgraded/downgradedautomatically.

npm将自动升级/降级

n (macOS, Linux)

n (macOS、Linux)

nis most likely to rvm(Ruby Version Manager), and is used to manage node.jsand npmversions simultaneously. It is written on pure Linux shell, and available as an npm module. So, if you already have any node.jsversion installed, you can install/updatethe npackage through npm:

n最有可能是rvm(Ruby 版本管理器),用于同时管理node.jsnpm版本。它是在纯Linux shell上编写的,可作为npm 模块使用。所以,如果你已经有任何的node.js安装的版本,你可以安装/更新ñ通过包npm

$ npm install -g n

Downloading, installing and switching to node.jsand npmversions is as easy as:

下载、安装和切换到node.jsnpm版本非常简单:

$ n 0.10.26
$ n 0.8.17
$ n 0.9.6

To download, install, and switch to the latestofficial release, use:

要下载、安装并切换到最新的官方版本,请使用:

$ n latest

To download, install, and switch to the latest stableofficial release, use:

要下载、安装并切换到最新的稳定官方版本,请使用:

$ n stable

To switch to the previously active version (aka $ cd -), use:

要切换到以前的活动版本(又名$ cd -),请使用:

$ n prev

If you want to see the list of installed node.jsversions, just run nfrom your command line. The output will be something like the following:

如果您想查看已安装的node.js版本列表,只需从命令行运行即可n。输出将类似于以下内容:

$ n

  0.10.26
? 0.8.17
  0.9.6

Where the dot (?) means that it's a currently active version. To select another node.jsversion from the list, use Up/Downarrow keys and activate using the Enterkey.

其中点 (?) 表示它是当前活动的版本。要从列表中选择另一个node.js版本,请使用Up/Down箭头键并使用 键激活Enter

To list the versions available to install:

列出可安装的版本:

$ n lsr

nvm (macOS, Linux)

nvm (macOS, Linux)

nvmis also like rvm, even the command names and usage are very similar.

nvm也和rvm类似,甚至命令名称和用法都非常相似。

To install nvmyou can use the installation script(requires git) using cURL:

要安装nvm,您可以使用安装脚本(需要gitcURL

$ curl https://raw.github.com/creationix/nvm/master/install.sh | sh

or wget:

wget

$ wget -qO- https://raw.github.com/creationix/nvm/master/install.sh | sh

To download and install a specific node.jsand npmversion, use:

要下载并安装特定的node.jsnpm版本,请使用:

$ nvm install 0.10

Then, you can switch to the installed version, using:

然后,您可以切换到已安装的版本,使用:

$ nvm use 0.10

Also, you can create the .nvmrcfile containing the version number, then switch to the specified version using the following command:

此外,您可以创建.nvmrc包含版本号的文件,然后使用以下命令切换到指定的版本:

$ nvm use

To see the list of installed node.jsversions, use:

要查看已安装的node.js版本列表,请使用:

$ nvm ls

To list the versions available to install:

列出可安装的版本:

$ nvm ls-remote

nvm-windows (Windows)

nvm-windows (Windows)

nvm-windowsis a node.jsversion management utility for Windows, ironically written in Go.

nvm-windows是一个用于 Windows的node.js版本管理实用程序,具有讽刺意味的是用Go编写。

It is not the same thing as nvm. However, the usage as a node.jsversion manager is very similar.

它与nvm 不同。但是,作为node.js版本管理器的用法非常相似。

To install nvm-windows, it is required to uninstall any existing versions of node.jsand npmbeforehand. Then, download and run the latest installer from releases.

要安装nvm-windows,需要事先卸载node.jsnpm 的任何现有版本。然后,从releases下载并运行最新的安装程序

To upgrade nvm-windows, run the new installer. It will safely overwrite the files it needs to update without touching your node.jsinstallations.

要升级nvm-windows,请运行新的安装程序。它将安全地覆盖它需要更新的文件,而不会触及您的node.js安装。

nvm-windowsruns in an Admin shell. You'll need to start Powershellor Command Promptas Administratorto use nvm-windows.

nvm-windows在管理 shell 中运行。你需要启动PowerShell的命令提示符作为管理员使用NVM窗口

Before using, you may also need to enable nvm-windowswith the following command:

在使用之前,您可能还需要使用以下命令启用nvm-windows

C:\> nvm on

To download and install a specific node.jsand npmversion, use:

要下载并安装特定的node.jsnpm版本,请使用:

C:\> nvm install 0.12

Then, you can switch to the installed version, using:

然后,您可以切换到已安装的版本,使用:

C:\> nvm use 0.12

If you want to see the list of installed node.jsversions, use:

如果要查看已安装的node.js版本列表,请使用:

C:\> nvm list

To list the versions available to install:

列出可安装的版本:

C:\> nvm list available

回答by guya

Sometimes it's just simpler to download the latest version from http://nodejs.org/

有时从http://nodejs.org/下载最新版本更简单

Especially when all other options fail.

特别是当所有其他选项都失败时。

http://nodejs.org/-> click INSTALL -> you'll have the latest node and npm

http://nodejs.org/-> 点击 INSTALL -> 你将拥有最新的 node 和 npm

Simple!

简单的!

回答by Nick Benes

Just listened to an interview with the npm team on the latest episode of nodeup, and they recommended notusing updatefor the update from 1.xto 2.x. Instead, use:npm install npm -g

只是听与NPM队的最新一集的采访nodeup,和他们建议使用update从更新1.x2.x。相反,使用:npm install npm -g

回答by Arkadiusz Ros?aniec

$ npm install -g npm stable

Worked for me to update from 1.4.28 to 2.1.5

为我工作从 1.4.28 更新到 2.1.5

回答by kayz1

Upgrading for Windows Users

为 Windows 用户升级

Windows users should read Troubleshooting > Upgrading on Windowsin the npm wiki.

Windows 用户应该阅读npm wiki 中的故障排除 > 在 Windows升级

Upgrading on windows 10 using PowerShell (3rd party edit)

使用 PowerShell 在 Windows 10 上升级(第 3 方编辑)

The link above Troubleshooting#upgrading-on-windowspoints to a github page npm-windows-upgradethe lines below are quotes from the readme. I successfully upgraded from npm 2.7.4 to npm 3.9.3 using node v5.7.0 and powershell (presumably powershell version 5.0.10586.122)

上面的故障排除#upgrading-on-windows 链接指向一个 github 页面npm-windows-upgrade下面的行是自述文件中的引用。我使用 node v5.7.0 和 powershell(大概是 powershell 版本 5.0.10586.122)成功地从 npm 2.7.4 升级到 npm 3.9.3

First, ensure that you can execute scripts on your system by running the following command from an elevated PowerShell. To run PowerShell as Administrator, click Start, search for PowerShell, right-click PowerShell and select Run as Administrator.

首先,通过从提升的 PowerShell 运行以下命令,确保您可以在系统上执行脚本。要以管理员身份运行 PowerShell,请单击开始,搜索 PowerShell,右键单击 PowerShell 并选择以管理员身份运行。

Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force    

Then, to install and use this upgrader tool, run (also from an elevated PowerShell or cmd.exe):

然后,要安装和使用此升级程序工具,请运行(也从提升的 PowerShell 或 cmd.exe):

npm install --global --production npm-windows-upgrade
npm-windows-upgrade