Node.js 在 Debian 上日志报错,原因很多,通常要结合具体报错信息来看。下面我把常见原因 + 对应日志表现 + 解决思路给你整理一下,你可以对照看。
日志表现
SyntaxError: Unexpected token ...
Error: Cannot find module 'xxx'
原因
✅ 解决
# 查看版本
node -v
# 推荐用 NodeSource 安装新版本
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt install -y nodejs
日志表现
Error: listen EADDRINUSE :::3000
Error: ENOENT: no such file or directory
原因
✅ 解决
journalctl -u your-node-service -n 50 --no-pager
检查服务文件:
WorkingDirectory=/var/www/app
User=node
Environment=NODE_ENV=production
日志表现
EACCES: permission denied
原因
✅ 解决
chown -R node:node /var/www/app
或避免使用 sudo 装 npm 包:
npm config set prefix ~/.npm-global
日志表现
Cannot find module 'express'
✅ 解决
rm -rf node_modules package-lock.json
npm install
日志表现
502 Bad Gateway
connect() failed (111: Connection refused)
✅ 排查
curl http://localhost:3000
journalctl -u nginx
日志表现
Killed
<--- Last few GCs --->
✅ 解决
free -h
dmesg | grep -i oom
请贴出 完整报错日志(一两行即可),例如:
systemd[1]: Started Node.js App.
app[1234]: Error: listen EADDRINUSE: address already in use :::3000
我可以直接帮你精确定位。
cat /etc/os-release)node -v)我可以一步一步帮你排到彻底解决 ✅