在 Debian 上配置 Node.js,常见且推荐的方式有 apt 安装、NodeSource 仓库、NVM(Node Version Manager)。下面按使用场景逐一说明,并给出推荐做法。
适合:只是临时用、对版本要求不高。
sudo apt update
sudo apt install -y nodejs npm
查看版本:
node -v
npm -v
特点:
适合:服务器、生产环境,需要较新 LTS 版本。
sudo apt update
sudo apt install -y curl
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 --lts
或使用指定版本:
nvm install 18
nvm alias default lts/*
✅ 优点:
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
在 ~/.bashrc 中添加:
export PATH=~/.npm-global/bin:$PATH
然后:
source ~/.bashrc
npm config set registry https://registry.npmmirror.com
node -v
npm -v
which node
| 场景 | 推荐方式 |
|---|---|
| 服务器 / 生产 | NodeSource |
| 本地开发 | NVM |
| 快速测试 | apt |
如果你愿意,可以告诉我:
我可以给你 更精确的配置方案 或 一键脚本。