Debian/Ubuntu 安装 NodeJS (使用nodesource)

参考: https://nodejs.org/en/download/package-manager#debian-and-ubuntu-based-linux-distributions

开源地址: https://github.com/nodesource/distributions?tab=readme-ov-file#debian-and-ubuntu-based-distributions

1. 安装

建议安装LTS版本: Node.js v20.x:

1
2
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - &&\
sudo apt-get install -y nodejs

检查版本:

1
2
node -v
npm -v

2. NPM 换源

推荐使用 nrm 来管理镜像源。设置教程参考此文章:https://www.51shizhi.com/article/a/nrm-npm-huanyuan

3. 更新

1
2
sudo apt upgrade nodejs  # 更新 nodejs 软件包
sudo npm install npm@latest -g # 更新 npm

4. 卸载

卸载 nodejs 软件包和软件源:

1
2
3
apt-get remove --purge nodejs &&\
rm -r /etc/apt/sources.list.d/nodesource.list &&\
rm -r /etc/apt/keyrings/nodesource.gpg