在 Debian 系统上升级 Node.js 有多种方式,推荐使用 NodeSource 或 nvm。下面按常见场景给你详细说明。
node -v
npm -v
适合服务器、生产环境。
sudo apt remove nodejs npm -y
sudo apt autoremove -y
sudo apt update
sudo apt install -y curl
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
可替换版本号:
setup_18.xsetup_20.xsetup_21.xsudo apt install -y nodejs
node -v
npm -v
✅ 优点:稳定、适合服务器
❌ 缺点:切换版本不如 nvm 方便
适合开发环境、需要多版本切换。
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
重新加载 shell:
source ~/.bashrc
# 或
source ~/.zshrc
nvm ls-remote
nvm install 20
或安装最新 LTS:
nvm install --lts
nvm alias default 20
node -v
✅ 优点:多版本共存、切换方便
❌ 缺点:仅对当前用户生效
n 模块(不推荐新手)npm install -g n
sudo n lts
⚠️ 在 Debian 上容易和 apt 安装的 Node 冲突,不推荐。
node -v 没变?which node
可能是 PATH 问题,nvm 用户请确认:
source ~/.bashrc
Debian 官方源 Node 版本通常很旧,强烈建议用 NodeSource 或 nvm。
| 场景 | 推荐方式 |
|---|---|
| 服务器 / 生产环境 | ✅ NodeSource |
| 本地开发 | ✅ nvm |
| 临时测试 | n |
如果你愿意,可以告诉我:
我可以给你最合适的具体升级命令。