在Ubuntu上更新Node.js,您可以使用NodeSource存储库或NVM(Node Version Manager)。以下是使用这两种方法的详细步骤:
方法1:使用NodeSource存储库
sudo apt-get remove --purge nodejs
sudo apt-get autoremove
curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -
请注意,您可以将14.x
替换为您想要安装的Node.js版本,例如12.x
、16.x
等。
sudo apt-get update
sudo apt-get install -y nodejs
node --version
npm --version
方法2:使用NVM(Node Version Manager)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
source ~/.bashrc
nvm list-remote
nvm install --lts
nvm use --lts
node --version
npm --version
使用这些方法之一更新Node.js后,您将拥有最新版本的Node.js和npm。如果您想要切换到其他版本,只需使用NodeSource存储库或NVM进行相应的操作即可。