在 Debian 上配置 Node.js 常见有 三种方式:
1️⃣ 使用 Debian 官方源(最简单,但版本较旧)
2️⃣ 使用 NodeSource(推荐,版本新且稳定)
3️⃣ 使用 nvm(适合开发,多版本切换)
下面分别说明。
sudo apt update
sudo apt install -y nodejs npm
查看版本:
node -v
npm -v
⚠️ Debian 11/12 中的 Node 版本通常较旧(如 12/18)。
以 Node.js 20 LTS 为例:
sudo apt update
sudo apt install -y curl ca-certificates gnupg
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
验证:
node -v
npm -v
✅ 优点:
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
✅ 优点:
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
node -e "console.log('Node is working')"
如果你告诉我:
cat /etc/os-release)我可以给你更精确的配置方案。