在 CentOS 系统中更新 Node.js 到最新版本,你可以选择使用 NodeSource 仓库或者 NVM(Node Version Manager)。下面是两种方法的详细步骤:
方法一:使用 NodeSource 仓库
sudo yum remove nodejs
sudo yum groupinstall "Development Tools"
curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash -
sudo yum install nodejs
node -v
npm -v
方法二:使用 NVM(Node Version Manager)
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
~/.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。
nvm install node
nvm use node
node -v
npm -v
现在你已经成功地将 CentOS 系统中的 Node.js 更新到了最新版本。如果你想切换到其他版本,只需使用 nvm use <version> 命令。