温馨提示×

CentOS Node.js 如何进行版本升级

小樊
57
2025-08-12 14:52:53
栏目: 编程语言

在 CentOS 上升级 Node.js 版本,你可以选择使用 NodeSource Repository 或者 NVM(Node Version Manager)。下面是两种方法的详细步骤:

方法一:使用 NodeSource Repository

  1. 首先,移除已安装的 Node.js(如果有的话):
sudo yum remove nodejs
  1. 安装必要的开发工具:
sudo yum groupinstall "Development Tools"
  1. 安装 NodeSource Repository。以 Node.js 14.x 为例:
curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash -

你可以将 setup_14.x 替换为你想要安装的 Node.js 版本,例如 setup_12.x

  1. 安装 Node.js:
sudo yum install nodejs
  1. 验证安装:
node -v
npm -v

方法二:使用 NVM(Node Version Manager)

  1. 安装 NVM:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash

或者

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
  1. 激活 NVM:
source ~/.bash_profile

或者

source ~/.zshrc

(取决于你使用的 shell)

  1. 安装最新版本的 Node.js:
nvm install node
  1. 切换到最新版本:
nvm use node
  1. 验证安装:
node -v
npm -v

现在你已经成功地在 CentOS 上升级了 Node.js 版本。如果你想要切换到其他版本,只需使用 nvm use <version> 命令。

0