Hello! 欢迎来到小浪云!


CentOS如何升级Node.js版本


centos系统上升级node.js,您可以选择使用nodesource仓库或nvm (node version manager)两种方法。以下步骤将详细指导您如何操作:

方法一:使用NodeSource仓库

  1. 卸载现有Node.JS (如有): 首先,移除任何已安装的Node.js版本:
sudo yum remove nodejs
  1. 安装必要软件包: 安装Node.js所需的依赖项:
sudo yum install -y curl gcc-c++ make
  1. 添加NodeSource仓库:命令添加Node.js LTS (长期支持)版本的仓库。如需其他版本,请替换lts/*为目标版本号,例如14.x。
curl -sL https://rpm.nodesource.com/setup_lts.x | sudo bash -
  1. 安装Node.js: 安装Node.js:
sudo yum install -y nodejs
  1. 验证版本: 检查安装的Node.js版本:
node -v

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

  1. 卸载现有Node.js (如有): 与方法一相同,先移除已安装的Node.js:
sudo yum remove nodejs
  1. 安装NVM: 使用以下命令安装最新版本的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: 使NVM生效:
source ~/.bash_profile

或者:

source ~/.bashrc
  1. 安装Node.js版本: 使用NVM安装所需Node.js版本。例如,安装最新LTS版本:
nvm install --lts

安装特定版本,例如14.x:

nvm install 14.x
  1. 切换Node.js版本: 切换到指定的Node.js版本:
nvm use 14.x

(将14.x替换为您需要的版本号)

  1. 验证版本: 确认已切换的Node.js版本:
node -v

通过以上步骤,您已成功在centos上升级或安装Node.js。 使用NVM可以方便地管理多个Node.js版本,需要切换时只需使用nvm use 命令即可。

相关阅读