When a Node.js app hits an error nothing catches, Node prints the stack trace and exits, so your site goes down. The stack trace is the answer: it names the error and the line. Find it in your process manager's or platform's logs. If there's no stack trace at all, the process was probably killed from outside, usually by the system running out of memory. Either way, run Node under a process manager that restarts it, so one crash isn't an outage.
What's the problem
Your Node.js app stops, the site gives 502 errors or doesn't respond, and the logs end with a stack trace, or just stop.
Why it happens
- An uncaught exception: an error thrown outside any
try/catch. - An unhandled promise rejection: an
asynccall that failed without acatch. - Out of memory: a leak or a large job, ending in the system killing the process.
- A missing environment variable or dependency at startup.
- A port already in use when restarting.
How to fix it
- Find the last stack trace in your logs (PM2, systemd's
journalctl, Docker, or your platform). - Fix the error at that line, and handle errors in async code.
- If there's no trace, check for out-of-memory kills. See out of memory.
- Run under a process manager (PM2, systemd, Docker restart policy) so it restarts automatically.
- Check startup configuration if it crashes immediately.
What Preventionlabs checks
One click runs the full report on your domain. For this problem it shows whether your site loads, how fast, and where its redirects go; up to 50 pages from your sitemap, listing any that fail or are slow, plus broken links on them. With Cloudflare connected, it also shows which pages returned server errors (5xx) to visitors, and when; recent deploys and account changes, with their times. It doesn't read your server logs, so the exact error message is in your host's logs.
Run your free reportYour first report is free, no card needed. Then A$50 a month, up to 10 domains. Read-only: it never changes anything.
Sources
- Node.js: Processofficial docs
By default, Node.js handles such exceptions by printing the stack trace to stderr and exiting with code 1, overriding any previously set process.exitCode .
- The Linux Kernel documentation: Documentation for /proc/sys/vm/official docs
If this is set to zero, the OOM killer will scan through the entire tasklist and select a task based on heuristics to kill. This normally selects a rogue memory-hogging task that frees up a large amount of memory when killed.
Preventionlabs is independent and not affiliated with Node.js. Product names are used only to describe the problem.
← All of Frameworks