下面按常见使用场景给你讲一下在 Debian 下配置 Node.js 的方法,从“最推荐”到“手动编译”都覆盖。
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 -
其他版本把
20.x换成18.x/22.x即可
sudo apt install -y nodejs
node -v
npm -v
sudo apt install nodejs npm
⚠️ 缺点:
适合经常切换 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
❌ 不要随便用 sudo npm -g
✅ 推荐方式:
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 app.js
npm i -g pm2
pm2 start app.js
你可以说:
我可以直接给你一套完整配置方案。