在 Debian 上配置 Node.js 有多种方式,最推荐的是使用 NodeSource 或 nvm(Node Version Manager)。下面按常见场景给你详细介绍。
sudo apt update && sudo apt upgrade -y
sudo apt install -y curl gnupg
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
Node.js 版本可根据需要调整:
setup_18.xsetup_20.xsetup_21.xsudo 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
或(zsh):
source ~/.zshrc
nvm install 20
nvm use 20
nvm alias default 20
node -v
npm -v
Debian 仓库中的 Node.js 版本通常较旧:
sudo apt update
sudo apt install nodejs npm
查看版本:
node -v
⚠️ 不推荐用于生产环境。
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 | 服务器 / 生产 | 稳定、官方支持 |
| nvm | 开发环境 | 多版本切换 |
| apt | 快速测试 | 简单但版本旧 |
如果你告诉我:
我可以给你更精确的配置方案 ✅