在 Debian 上配置 Node.js,通常分为安装和基础配置/环境管理两部分。下面按常见场景给你说明。
# 以 Node.js 20.x 为例
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
验证:
node -v
npm -v
sudo apt update
sudo apt install nodejs npm
适合对版本要求不高的场景。
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install --lts
nvm use --lts
优点:
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
npm config set registry https://registry.npmmirror.com
查看:
npm config get registry
npm install -g yarn pnpm
node app.js
后台运行:
nohup node app.js &
或使用 PM2:
npm install -g pm2
pm2 start app.js
sudo ufw allow 3000
如果你能说明:
我可以给你更精确的配置方案。