Debian 上 Node.js 启动失败 的原因有很多,常见原因和排查方法如下(按出现频率排序):
现象
node: command not found
排查
which node
node -v
解决
sudo apt update
sudo apt install -y nodejs npm
✅ 推荐安装新版本(Debian 自带版本较旧):
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
现象
SyntaxError: Unexpected token ...
原因
解决
node -v
# 如果是 v10 或更低,建议升级
使用 nvm 安装新版(推荐):
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install 20
nvm use 20
现象
Error: listen EADDRINUSE: address already in use :::3000
排查
sudo lsof -i :3000
解决
kill -9 PID
或换端口:
app.listen(3001);
现象
EACCES: permission denied
排查
whoami
ls -l yourfile.js
解决
sudo node(除非真的需要)sudo chown -R $USER:$USER /your/project
现象
sudo systemctl status mynode
常见错误
ExecStart 路径错误正确示例
[Service]
ExecStart=/usr/bin/node /var/www/app/index.js
Restart=always
User=nodeuser
✅ 用绝对路径,不要用 node 直接写
现象
Error: Cannot find module 'express'
解决
rm -rf node_modules package-lock.json
npm install
现象
cannot execute binary file
排查
uname -m
node -v
✅ 使用对应架构的 Node.js
node -v
npm -v
which node
df -h # 磁盘满?
free -h # 内存不足?
journalctl -xe
请直接贴出 完整报错信息,例如:
systemctl status xxxnode app.js 的输出cat /etc/os-release)我可以 1 分钟帮你判断是 版本 / 端口 / 权限 / systemd / 依赖 哪一类问题。