在 Debian 上配置 Node.js 通常有以下几种常见方式,下面按推荐程度给你说明。
这是最常用、版本新、最稳定的方式。
sudo apt update
sudo apt upgrade -y
sudo apt install -y curl gnupg2 ca-certificates lsb-release
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
可选版本:
- Node 18:
setup_18.x- Node 21:
setup_21.x
sudo apt install -y nodejs
node -v
npm -v
版本通常较旧,仅适合测试。
sudo apt update
sudo apt install -y nodejs npm
查看版本:
node -v
适合多版本切换或普通用户使用。
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
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
npm install
npm run start
# /etc/systemd/system/nodeapp.service
[Unit]
Description=Node App
After=network.target
[Service]
User=www-data
WorkingDirectory=/var/www/app
ExecStart=/usr/bin/node index.js
Restart=always
[Install]
WantedBy=multi-user.target
sudo systemctl enable nodeapp
sudo systemctl start nodeapp
如果你能告诉我:
我可以给你更精确的配置方案。