在 Debian 系统上配置 Node.js 有多种方式,推荐优先使用 NodeSource 或 nvm,具体取决于你是“系统级使用”还是“开发环境”。下面按常用方式详细说明。
适合:服务器、生产环境
sudo apt update
sudo apt upgrade -y
sudo apt install -y curl ca-certificates gnupg
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /usr/share/keyrings/nodesource.gpg
NODE_MAJOR=20
echo "deb [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
sudo apt update
sudo apt install -y nodejs
node -v
npm -v
适合:前端 / 后端开发,多版本切换
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
或使用 wget:
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
# 或
source ~/.zshrc
nvm install --lts
或指定版本:
nvm install 20
nvm alias default 20
node -v
npm -v
版本通常较旧:
sudo apt update
sudo apt install -y nodejs npm
查看版本:
node -v
⚠️ Debian 12 之前版本 Node.js 可能低于 18,不建议用于现代项目。
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 -e "console.log('Node.js 配置成功')"
node 命令不存在?source ~/.bashrcsudo npm install -g| 场景 | 推荐方式 |
|---|---|
| 服务器 / 生产 | NodeSource |
| 本地开发 | nvm |
| 快速测试 | Debian 官方仓库 |
如果你愿意,可以告诉我:
我可以给你一套完整部署方案。