温馨提示×

centos下如何更新nodejs到最新版本

小樊
40
2025-10-30 04:58:54
栏目: 编程语言

在 CentOS 系统中更新 Node.js 到最新版本,你可以选择使用 NodeSource 仓库或者 NVM(Node Version Manager)。下面是两种方法的详细步骤:

方法一:使用 NodeSource 仓库

  1. 首先,移除已安装的 Node.js(如果有的话):
sudo yum remove nodejs
  1. 安装必要的开发工具:
sudo yum groupinstall "Development Tools"
  1. 安装 NodeSource 仓库。以下命令将添加 Node.js 14.x 的仓库,你可以根据需要更改版本号:
curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash -
  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.39.0/install.sh | bash

或者

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
  1. 激活 NVM。在你的 shell 配置文件(如 ~/.bashrc~/.bash_profile~/.zshrc)中添加以下行:
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

然后运行 source ~/.bashrc(或相应的配置文件)以激活 NVM。

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

现在你已经成功地将 CentOS 系统中的 Node.js 更新到了最新版本。如果你想切换到其他版本,只需使用 nvm use <version> 命令。

0