在Debian中管理Node.js版本,你可以使用NodeSource存储库或者nvm(Node Version Manager)
方法1:使用NodeSource存储库
- 首先,删除已安装的Node.js(如果有的话):
sudo apt-get remove --purge nodejs sudo apt-get autoremove
- 安装必要的软件包:
sudo apt-get update sudo apt-get install curl software-properties-common
- 添加NodeSource存储库。选择你需要的Node.js版本,例如14.x:
curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -
- 安装Node.js:
sudo apt-get install -y nodejs
- 验证安装:
node --version npm --version
方法2:使用nvm(Node Version Manager)
- 首先,卸载已安装的Node.js(如果有的话):
sudo apt-get remove --purge nodejs sudo apt-get autoremove
- 安装nvm。运行以下命令,将nvm安装到你的用户目录:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
- 激活nvm。将以下内容添加到你的
~/.bashrc
、~/.zshrc
或~/.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" # This loads nvm
然后运行source ~/.bashrc
(或相应的配置文件)以激活nvm。
- 使用nvm安装Node.js。例如,安装最新的LTS版本:
nvm install --lts
- 切换到你需要的Node.js版本:
nvm use
将
替换为你需要的版本,例如14.17.0
。
- 验证安装:
node --version npm --version
现在你已经成功地在Debian中管理了Node.js版本。你可以随时切换到其他版本,而无需重新安装。