在 CentOS 系统中,更新 Node.js 的方法有以下几种:
方法一:使用 NodeSource Repository
sudo yum remove nodejs
<version>
(例如:14.x、16.x 等):curl -sL https://rpm.nodesource.com/setup_<version> | 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
source ~/.bash_profile
nvm install node
node -v
npm -v
nvm install <version>
nvm use <version>
请注意,使用 NVM 安装的 Node.js 不需要使用 sudo
。
更新 Node.js 时,只需重复上述步骤并安装所需版本即可。