Course contentsShow
← Node.js
Lesson 1508 of 1,98013. Error Handling and Resilient SystemsFree lesson

Configuring Node.js Core Dumps

You will set up the OS to generate core dumps on fatal errors for low-level investigation of runtime internals.

In this lesson, you will learn how to configure your operating system to generate core dumps when a Node.js process crashes, enabling post-mortem analysis of the process memory.

The Role of Core Dumps

When a Node.js process terminates unexpectedly—due to a segmentation fault, an unhandled exception in native C++ addons, or a signal like SIGQUIT—the process state vanishes. Standard logs provide a trail of high-level events, but a core dump provides a snapshot of the process's entire memory space. This allows you to use tools like lldb or gdb to inspect the heap, examine call stacks, and identify the exact state of variables at the moment of failure.

Preparing the Environment

By default, most Linux distributions suppress core dumps to prevent disk bloat. Before your Node.js application can generate these files, you must configure the OS limits. You can check your current limit with ulimit -c. If it returns 0, core dumps are disabled.

To enable them, update your shell environment or global system settings (e.g., in /etc/security/limits.conf):

# Allow core files of unlimited size
ulimit -c unlimited

Triggering Dumps with Node.js

Once the OS limit is lifted, you can force Node.js to generate a core dump upon receiving specific signals. You can use the process module to listen for signals like SIGQUIT and invoke process.abort() to terminate immediately and trigger the dump:

process.on('SIGQUIT', () => {
  console.error('Received SIGQUIT. Generating core dump...');
  process.abort(); // Terminates the process and triggers a core file
});

When process.abort() is called, Node.js triggers a SIGABRT signal, which the OS handles by writing the memory image to the disk. By default, this file is named core or core.<pid> in your current working directory.

A Note on Production

Be cautious: core dumps contain the entire memory contents of your process, which may include sensitive user data, encryption keys, or authentication tokens. Ensure these files are stored in a secure, restricted directory and follow your organization's data retention policies.

Key Takeaway: Configuring the OS to permit unlimited core files via ulimit and using process.abort() allows you to capture a complete memory snapshot for debugging otherwise unreproducible low-level crashes.