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

How to properly upgrade node using nvm

5 个月前提问
3 个月前修改
浏览次数270

6个答案

1
2
3
4
5
6

首先,nvm(Node Version Manager)是一个用于管理多个Node.js版本的工具,它允许用户轻松切换和安装不同的Node.js版本。以下是使用nvm升级Node.js版本的步骤:

  1. 安装或确认安装了 nvm: 要使用nvm升级Node.js,您首先需要确认您的系统上已经安装了nvm。可以通过在终端中运行以下命令来检查:

    shell
    nvm --version

    如果您还没有安装nvm,可以访问官方的nvm GitHub仓库来查看安装说明。

  2. 列出可用的 Node.js 版本: 要检查可用的Node.js版本,您可以使用nvm列出所有远程服务器上的版本:

    shell
    nvm ls-remote

    这个命令会列出所有可用的版本,包括LTS(长期支持)版本。

  3. 安装新的 Node.js 版本: 当您决定要安装一个特定版本的Node.js时,可以使用以下命令:

    shell
    nvm install <version>

    您可以将<version>替换为特定的版本号,例如14.17.0,或者使用node代表最新的稳定版本:

    shell
    nvm install node

    这将安装最新的稳定Node.js版本。

  4. 切换至新版本: 安装完新版本后,您可以使用如下命令切换到新版本:

    shell
    nvm use <version>

    类似地,将<version>替换为您刚才安装的版本号,或者使用node来选择最新安装的版本。

  5. 确认新版本的 Node.js: 完成上述步骤后,您可以通过以下命令确认当前使用的Node.js版本:

    shell
    node --version

    这应该显示您刚才选择的版本。

  6. 升级 npm(如果需要的话): 某些时候,您可能还需要升级npm(Node包管理器)。您可以使用以下命令来升级npm

    shell
    npm install -g npm
  7. 设置默认 Node.js 版本: 如果您希望每次打开一个新的终端时都使用新版的Node.js,可以将它设置为默认版本:

    shell
    nvm alias default <version>
  8. 如果有必要,重新安装全局包: 有时候,升级Node.js后,您可能需要重新安装全局Node包。您可以通过查看当前版本的全局包,然后在新版本中重新安装它们。

    shell
    nvm ls-remote --reinstall-packages-from=<old_version>

    <old_version>替换为您之前使用的Node.js版本。

通过这些步骤,您应该能够在不影响旧版Node.js及其依赖的情况下,安全且正确地使用nvm升级Node.js版本。

2024年6月29日 12:07 回复

This may work:

shell
nvm install NEW_VERSION --reinstall-packages-from=OLD_VERSION

For example:

shell
nvm install 6.7 --reinstall-packages-from=6.4

then, if you want, you can delete your previous version with:

shell
nvm uninstall OLD_VERSION

Where, in your case, NEW_VERSION = 5.4 OLD_VERSION = 5.0

Alternatively, try:

shell
nvm install node --reinstall-packages-from=current

Or you can update to the last long-term support version with

shell
nvm install lts --reinstall-packages-from=current
2024年6月29日 12:07 回复

You can more simply run one of the following commands:

Latest version:

shell
nvm install node --reinstall-packages-from=node

Stable (LTS) version: (if currently in use)

shell
nvm install "lts/*" --reinstall-packages-from="$(nvm current)"

This will install the appropriate version and reinstall all packages from the currently used node version.

This saves you from manually handling the specific versions.


Kudos to @m4js7er for commenting about the LTS version.

2024年6月29日 12:07 回复

TWO Simple Solutions:

To install the latest version of node and reinstall the old version packages just run the following command.

shell
nvm install node --reinstall-packages-from=node

To install the latest lts (long term support) version of node and reinstall the old version packages just run the following command.

shell
nvm install --lts /* --reinstall-packages-from=node

Here's a GIF animation to support this answer:

nvm

2024年6月29日 12:07 回复

Here are the steps that worked for me for Ubuntu OS and using nvm

Go to nodejs website and get the last LTS version (for example the version will be: x.y.z)

shell
nvm install x.y.z # In my case current version is: 14.15.4 (and had 14.15.3)

After that, execute nvm list and you will get list of node versions installed by nvm.

Now you need to switch to the default last installed one by executing:

shell
nvm alias default x.y.z

List again or run nvm --version to check:

enter image description here

Update: sometimes even if i go over the steps above it doesn't work, so what i did was removing the symbolic links in /usr/local/bin

shell
cd /usr/local/bin sudo rm node npm npx

And relink:

shell
sudo ln -s $(which node) /usr/local/bin/nodesudo && ln -s $(which npm) /usr/local/bin/npmsudo && ln -s $(which npx) /usr/local/bin/npx
2024年6月29日 12:07 回复

if you have 4.2 and want to install 5.0.0 then

shell
nvm install v5.0.0 --reinstall-packages-from=4.2

the answer of gabrielperales is right except that he missed the "=" sign at the end. if you don't put the "=" sign then new node version will be installed but the packages won't be installed.

source: sitepoint

2024年6月29日 12:07 回复

你的答案