在 Debian 上配置 Node.js 常见有 三种方式,推荐优先使用官方源或 NodeSource(版本新、可控)。下面按常用程度给你说明。
适合需要指定 Node 版本(如 18 / 20 / 22)
sudo apt update
sudo apt install -y curl ca-certificates gnupg
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
其他版本把
20.x改成18.x/22.x即可
sudo apt install -y nodejs
node -v
npm -v
sudo apt update
sudo apt install -y nodejs npm
查看版本:
node -v
⚠️ Debian 稳定版中的 Node 通常较旧(如 12 / 18)
适合多版本切换
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install 20
nvm use 20
npm config set registry https://registry.npmmirror.com
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
| 场景 | 推荐方式 |
|---|---|
| 生产服务器 | NodeSource |
| 快速测试 | apt |
| 本地开发 | NVM |
如果你是 服务器部署 / Docker / 某个具体项目,可以告诉我,我给你更具体的配置方案。