在 Debian 下运行 Node.js 时“日志报错”并不是一个具体错误,而是现象。要定位原因,通常要从报错内容、运行方式、系统环境三方面看。下面我按常见场景给你总结一下,你可以对照自己的情况。
Debian 默认仓库里的 Node 版本往往很旧(如 10、12),而你的项目可能需要 16/18/20+。
典型报错:
SyntaxError: Unexpected token ?(可选链)ERR_OSSL_EVP_UNSUPPORTEDCannot find module 'xxx'解决方法:
node -v
如果版本过低:
# 使用 NodeSource
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt install -y nodejs
Debian 默认不用 root 跑服务。
典型报错:
EACCES: permission deniedlisten EADDRINUSE解决方法:
sudo chown -R youruser:youruser /var/log/yourapp
或用 systemd 指定用户:
User=www-data
如果你用 systemctl 启动 Node:
典型报错:
code=exited, status=1Main process exited检查:
journalctl -u yourapp.service -n 50 --no-pager
常见错误:
WorkingDirectory 不对ExecStart 路径错NODE_ENV示例:
[Service]
ExecStart=/usr/bin/node /opt/app/index.js
WorkingDirectory=/opt/app
Environment=NODE_ENV=production
典型报错:
Cannot find module 'express'Error: Cannot find module 'better-sqlite3'解决方法:
rm -rf node_modules package-lock.json
npm install
⚠️ 注意:Debian 上有些原生模块需要:
apt install -y build-essential python3
典型报错:
Error: listen EADDRINUSE :::3000
查端口:
ss -ltnp | grep 3000
解决:
kill -9 <pid>
很多 Node 项目自己写日志文件,但目录没建。
典型报错:
ENOENT: no such file or directory, open '/var/log/app.log'
解决:
mkdir -p /var/log/app
chown youruser:youruser /var/log/app
请按这个顺序来:
journalctl -u yourapp -n 100
# 或直接运行
node index.js
node -v
whoami
npm ls
你可以直接贴:
node -vcat /etc/os-release)我可以逐行帮你分析是哪一类问题,并给精确修复命令。