在 Debian 下配置 Node.js,推荐采用 官方源 / NodeSource 的方式,下面给你几种常用、可靠的方法(从推荐到补充)。
适用于 Debian 10 / 11 / 12,可安装指定 Node.js 版本(如 LTS)。
sudo apt update
sudo apt install -y curl build-essential
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
如需其他版本:
setup_18.xsetup_21.xsetup_22.xsudo apt install -y nodejs
node -v
npm -v
✅ 优点:
NVM 可以在同一台机器上切换多个 Node.js 版本。
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install 20
nvm use 20
nvm alias default 20
node -v
npm -v
✅ 适合:
sudo apt install nodejs npm
❌ 问题:
仅在 运行旧系统服务 时考虑。
mkdir -p ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
npm config set registry https://registry.npmmirror.com
node: command not foundsource ~/.bashrcnpm install -g npm@latest
| 场景 | 推荐方案 |
|---|---|
| 服务器 / 生产 | ✅ NodeSource |
| 本地开发 | ✅ NVM |
| 老旧系统 | 默认 apt(不推荐) |
如果你能告诉我:
我可以给你 更精确的配置方案 ✅