Back to results for “Error Handling”
Node.js › Advanced Node.js

What are Node.js's built-in diagnostic reports, and how do you generate one?

Advanced

Answer

A diagnostic report (enabled via --report-uncaught-exception, --report-on-signal, or triggered programmatically) is a JSON snapshot of the process's state at a point in time -- including JavaScript and native stack traces, heap statistics, loaded modules, environment variables, and resource usage -- useful for post-mortem analysis of crashes or hangs in production without needing to reproduce the issue.
// Trigger a report on demand from within the process
const { writeReport } = require('node:process').report;
writeReport('report.json');

// Or generate one automatically on uncaught exceptions
// node --report-uncaught-exception --report-on-fatalerror app.js
Real-world example A production service that occasionally hangs under load is configured with --report-on-signal so operators can send SIGUSR2 to the process during a hang, capturing a full diagnostic report for offline analysis without restarting or losing the in-progress state.

Common follow-ups: What specific information does a diagnostic report include that a simple stack trace doesn't?;How do you configure a report to be generated automatically on an unhandled promise rejection?