How to Crash a Node.js App on Purpose: Safe Testing
In this article
You can crash a Node.js app on purpose by triggering an uncaught exception, an unhandled promise rejection, or a deliberate process exit. The quickest method is to add process.exit(1) to a route or script, but throwing an error inside an asynchronous function without a catch also works. This is useful for testing crash recovery, monitoring alerts, and graceful shutdown logic. Before you start, make sure you are working in a development or staging environment, never in production, and that you have a way to restart the process (like a process manager such as PM2 or a container orchestrator).
What You'll Need
- A Node.js project you can safely modify (development or staging copy).
- Node.js installed (any current LTS version is fine; the methods here work across versions).
- A terminal or command prompt to run the app.
- Optional: a process manager (PM2, systemd, Docker) to observe automatic restarts.
- Optional: a monitoring tool to confirm the crash is detected.
Step-by-Step
- Create a test route or script. If you have an Express app, add a route like
app.get('/crash', (req, res) => { ... }). If you prefer a standalone script, create a new filecrash.js. - Choose your crash method. The simplest is to call
process.exit(1)inside the route handler. This immediately terminates the process with a non-zero exit code, simulating a hard crash. Alternatively, throw an uncaught error:throw new Error('Intentional crash')inside a synchronous handler. For asynchronous code, usePromise.reject(new Error('Intentional crash'))without a.catch()to trigger an unhandled rejection (Node.js 15+ exits by default; older versions may only warn). - Add the code. For example, in an Express app:
Save the file.app.get('/crash', (req, res) => { process.exit(1); }); - Start your app. Run
node app.js(or your entry file) in the terminal. If using a process manager, start it as usual (e.g.,pm2 start app.js). - Trigger the crash. Open a browser or use
curlto visithttp://localhost:3000/crash(adjust port). The process should exit immediately. In the terminal, you will see the process stop; if using PM2, it will log a restart. - Observe the behavior. Check your terminal for exit messages. If you have monitoring, confirm an alert fired. If using a process manager, verify it restarted the app (PM2 shows restart count).
- Clean up. Remove the test route or script when done to avoid accidental crashes later.
Common Mistakes
- Running in production. Always test in a safe environment. Crashing a live app causes downtime and data loss.
- Forgetting to remove the crash code. Leaving a
/crashroute in production is a security risk. Use environment checks or delete it after testing. - Assuming unhandled rejections crash on all Node versions. In Node.js 15 and later, unhandled promise rejections terminate the process by default. In older versions, they may only emit a warning. Use
process.exit(1)for a guaranteed crash. - Not accounting for process managers. If your app is managed by PM2 or systemd, it will restart automatically. That is expected; do not confuse it with a failure to crash.
- Using
process.exit(0). Exit code 0 signals success; use a non-zero code like 1 to indicate a crash.
Tips
- Use environment variables to guard crash routes:
if (process.env.NODE_ENV !== 'production') { ... }. - Test different crash types: synchronous throw, asynchronous rejection, and explicit exit. Each may be handled differently by your error-handling middleware.
- If you want to test graceful shutdown, listen for
process.on('SIGTERM')and log before exiting, then trigger a crash to see if cleanup runs. - For containerized apps, a crash will cause the container to exit; orchestration tools like Kubernetes will restart it based on restart policy.
- Document the test procedure so teammates know how to reproduce it safely.
Troubleshooting
- The app does not crash. Check that you are hitting the correct route and that the code is actually executed. Add a
console.logbefore the crash line. If using an unhandled rejection, ensure you are on Node.js 15+ or useprocess.exit(1)instead. - The process restarts immediately. This is likely due to a process manager (PM2, nodemon, systemd). Check the manager's logs to confirm a restart occurred. To observe the crash without restart, stop the manager first.
- No error output. A hard exit via
process.exit()does not print a stack trace. If you need to see an error, throw an uncaught exception instead. - Permission errors. Ensure you have write access to the project files and that the Node process can be terminated (e.g., not running as a different user).
- Port already in use. If the app fails to start, another process may be using the port. Stop it or change the port.
Frequently Asked Questions
Is it safe to crash my Node.js app on purpose?
Yes, in a development or staging environment. Never do it in production unless you are testing a controlled failover scenario with proper safeguards.
What is the difference between process.exit(1) and throwing an error?
process.exit(1) immediately terminates the process without running cleanup handlers. Throwing an uncaught error also terminates but may print a stack trace and allow some error events to fire.
Will an unhandled promise rejection always crash my app?
In Node.js 15 and later, yes, by default. In earlier versions, it may only log a warning. You can force a crash by adding process.on('unhandledRejection', () => { process.exit(1); }).
How do I crash a specific route without affecting others?
Add the crash logic only inside that route's handler. Other routes remain unaffected until the process exits.
Can I simulate a crash without modifying code?
You can send a signal like SIGKILL from the terminal (kill -9 <pid>), but that is an external kill, not an in-app crash. For testing in-app error handling, code modification is needed.
What exit code should I use for a crash?
Use a non-zero code, typically 1, to indicate an abnormal termination. Code 0 means success.