下面给你一份在 Debian 上配置 Node.js 环境的实用指南,从「最推荐」到「备选方案」都列出来,你可以按自己需求选。
适用于 Debian 10/11/12。
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 -
想装 18:
setup_18.x
想装 22:setup_22.x
sudo apt install -y nodejs
node -v
npm -v
sudo apt install -y nodejs npm
⚠️ 缺点:Debian 官方源里的 Node 版本通常较老,不适合现代项目。
适合需要切换 Node 版本的开发者。
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
npm config get registry
npm 命令不存在?sudo apt install -y npm
mkdir ~/.npm-global
npm config set prefix ~/.npm-global
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
| 使用场景 | 推荐方式 |
|---|---|
| 服务器部署 | NodeSource |
| 本地开发 | NVM |
| 快速测试 | apt |
如果你告诉我:
cat /etc/os-release)我可以给你更精确的一步到位命令。