在 Ubuntu 中升级 Node.js 的方法如下:
方法一:使用 NodeSource 存储库
sudo apt-get remove --purge nodejs
sudo apt-get autoremove
curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -
这里,我们使用的是 Node.js 14.x 版本。如果你想安装其他版本,请将 setup_14.x
替换为相应的版本,例如 setup_12.x
。
sudo apt-get update
sudo apt-get install -y nodejs
node --version
npm --version
方法二:使用 NVM(Node Version Manager)
sudo apt-get remove --purge nodejs
sudo apt-get autoremove
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
或者使用 wget:
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
nvm --version
nvm install node
nvm use node
node --version
npm --version
现在,你已经成功地在 Ubuntu 中升级了 Node.js。如果你想切换到其他版本的 Node.js,只需使用 nvm use <version>
命令。