在 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)中添加以下行: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
或者
source ~/.bash_profile
nvm install node
nvm use node
node -v
npm -v
现在你已经成功地在 CentOS 上更新了 Node.js 版本。如果你想切换到其他版本,只需使用 nvm use <version> 命令。