乐闻世界logo
搜索文章和话题

如何将 NodeJS 和 NPM 升级到最新版本?

7 个月前提问
5 个月前修改
浏览次数72

6个答案

1
2
3
4
5
6

要将Node.js和npm更新到最新的版本,需要遵循一系列步骤,这取决于您的操作系统以及您当前安装Node.js的方式。以下是一些通用的指导步骤,适用于多种操作系统:

使用包管理器更新

对于 macOS 和 Linux 用户:

  1. 使用Homebrew(如果是macOS并且之前通过Homebrew安装): 安装Homebrew:

    sh
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

    更新Node.js:

    sh
    brew update brew upgrade node
  2. 使用n或nvm(Node.js版本管理器): 安装n(简化版本管理器):

    sh
    npm install -g n

    使用n更新到最新的稳定版本:

    sh
    n stable

    或者安装nvm(Node版本管理器):

    sh
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash # 或者使用Wget: wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

    通过nvm安装最新的Node.js版本:

    sh
    nvm install node # "node"是指最新版本 nvm use node

对于Windows用户:

如果你是通过Windows包管理器如Chocolatey安装的,可以使用以下命令:

sh
choco upgrade nodejs

或者,您也可以使用nvm-windows,一个专为Windows环境设计的nvm版本:

sh
nvm install latest nvm use latest

手动更新

如果你没有使用任何包管理器,你可以手动下载最新的Node.js安装包:

  1. 访问Node.js官方网站 Node.js
  2. 根据您的操作系统选择相应的安装包下载。
  3. 运行下载的安装程序并遵循其指示以完成安装。

更新npm

通常,更新Node.js后,npm也会更新。但是,如果需要手动更新npm,可以使用以下命令:

sh
npm install -g npm@latest

这会将npm更新到最新的版本。

验证更新

安装完毕后,您可以运行下面的命令来验证Node.js和npm的版本:

sh
node -v npm -v

这两条命令将会显示您当前的Node.js和npm版本,从而验证更新是否成功。

请记住,更新到最新版本可能会导致与旧项目的兼容性问题,因此在更新之前最好备份您的项目。此外,某些项目可能依赖于特定的Node.js版本,所以在升级到最新的版本之前,请确保阅读项目文档以避免潜在的版本冲突。

2024年6月29日 12:07 回复

Use:

shell
npm update -g npm

See the documentation for the update command:

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

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

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

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

How do I update npm?

shell
npm install -g npm

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

You can also update all outdated local packages by doing npm update without 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:

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

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

2024年6月29日 12:07 回复

I found this really neat way of updating Node.js on David Walsh's blog. You can do it by installing n:

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

It will install the current stable version of Node.js.


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

shell
nvm ls-remote nvm install <version> nvm use <version>
2024年6月29日 12:07 回复

Updating npm is easy:

shell
npm install npm@latest -g
2024年6月29日 12:07 回复

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

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

2024年6月29日 12:07 回复

As you may already know, npm is currently bundled with Node.js. It means that if you have installed Node.js, you've already installed npm as well.

Also, pay attention to the Node.js and npm release versions table that shows us approximate versions compatibility. Sometimes, versions discrepancy may cause incompatibility errors.

So, if you're a developer, it's kind of "best practice" to manage your development environment using one of the Node.js version managers.

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

Homebrew (macOS)

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

Actually, it's not just a Node.js version manager.

To install Homebrew to your Mac:

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

To install Node.js and npm using Homebrew, run:

shell
brew install node

Later, you will be able to update them using:

shell
brew update && brew upgrade node

Also, you can switch between Node.js versions as well:

shell
brew switch node 0.10.26

npm will be upgraded/downgraded automatically.

n (macOS, Linux)

n is most likely to rvm (Ruby Version Manager), and is used to manage Node.js and npm versions simultaneously. It is written on pure Linux shell, and available as an npm module. So, if you already have any Node.js version installed, you can install/update the n package through npm:

shell
npm install -g n

Downloading, installing and switching to Node.js and npm versions is as easy as:

shell
n 0.10.26 n 0.8.17 n 0.9.6

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

shell
n latest

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

shell
n stable

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

shell
n prev

If you want to see the list of installed Node.js versions, just run n from your command line. The output will be something like the following:

shell
n

Output:

shell
0.10.26 0.8.17 0.9.6

Where the dot (•) means that it's a currently active version. To select another Node.js version from the list, use Up/Down arrow keys and activate using the Enter key.

To list the versions available to install:

shell
n lsr

nvm (macOS, Linux)

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

To install nvm, you can use the installation script (requires Git) using cURL:

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

or Wget:

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

To download and install a specific Node.js and npm version, use:

shell
nvm install 0.10

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

shell
nvm use 0.10

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

shell
nvm use

To see the list of installed Node.js versions, use:

shell
nvm ls

To list the versions available to install:

shell
nvm ls-remote

nvm-windows (Windows)

nvm-windows is a Node.js version management utility for Windows, ironically written in Go.

It is not the same thing as nvm. However, the usage as a Node.js version manager is very similar.

To install nvm-windows, it is required to uninstall any existing versions of Node.js and npm beforehand. Then, download and run the latest installer from releases.

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

nvm-windows runs in an Admin shell. You'll need to start PowerShell or Command Prompt as Administrator to use nvm-windows.

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

shell
nvm on

To download and install a specific Node.js and npm version, use:

shell
nvm install 0.12

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

shell
nvm use 0.12

If you want to see the list of installed Node.js versions, use:

shell
nvm list

To list the versions available to install:

shell
nvm list available
2024年6月29日 12:07 回复

你的答案