How to fix Error: kill ESRCH in Node.js

Node.jsINTERMEDIATEMEDIUMUpdated Dec 9, 2025ยท 153 views

This error occurs when attempting to kill a process that no longer exists or never existed. The ESRCH code indicates that the specified process ID (PID) cannot be found in the system.

Quick fix

Fixes ESRCH

function isProcessRunning(pid) {
  try {
    process.kill(pid, 0);
    return true;
  } catch (err) {
    return err.code !== 'ESRCH';
  }
}

// Use before killing
if (isProcessRunning(childPid)) {
  process.kill(childPid, 'SIGTERM');
}
  1. Check if process exists before killing
  2. Wrap process.kill() in try-catch
  3. Handle child process exit events properly
  4. Use AbortController for modern process management
node.js โ€” error
function isProcessRunning(pid) {
try {
process.kill(pid, 0);
return true;
} catch (err) {
return err.code !== 'ESRCH';
}
}
// Use before killing
if (isProcessRunning(childPid)) {
process.kill(childPid, 'SIGTERM');
}
Error: kill ESRCH

What this error means

The ESRCH error (Error: kill ESRCH) stands for "Error: no such process" and is thrown by Node.js when calling `process.kill()` or `subprocess.kill()` on a process ID that doesn't exist in the system. This error is part of Node.js's process management API and indicates that the system cannot find a process with the given PID. The error can occur with both the parent process and child processes spawned via the `child_process` module. ESRCH is a POSIX error code that Node.js surfaces when the underlying system call to terminate a process fails because the target process cannot be located. This commonly happens in asynchronous environments where process lifecycles are managed across multiple event loop iterations.

Typical symptoms

  • Uncaught Error: kill ESRCH thrown when calling process.kill()
  • Child process termination scripts fail unexpectedly
  • Process cleanup code throws errors in production
  • PM2 or other process managers report kill failures
  • VSCode debugger shows "Error processing terminate: Error: kill ESRCH"

Common causes

  1. Process has already exited before the kill signal was sent (race condition)
  2. Incorrect PID value passed to process.kill() or subprocess.kill()
  3. On Windows, attempting to kill a shell wrapper instead of the actual process
  4. Child process terminated between reading stdout and executing the kill command
  5. Using negative PID values on Windows to kill process groups (not supported)
  6. Process manager (PM2, cluster) attempting to kill already-terminated workers

How to fix "Error: kill ESRCH"

1Check if process exists before killing

Use process.kill(pid, 0) to test if a process exists without actually sending a kill signal. This method returns true if the process exists, or throws ESRCH if it doesn't:

javascript
function isProcessRunning(pid) {
  try {
    process.kill(pid, 0);
    return true;
  } catch (err) {
    return err.code !== 'ESRCH';
  }
}

// Use before killing
if (isProcessRunning(childPid)) {
  process.kill(childPid, 'SIGTERM');
}

This prevents the error by verifying the process exists before attempting termination.

2Wrap process.kill() in try-catch

Handle the ESRCH error gracefully instead of letting it crash your application:

javascript
try {
  process.kill(pid, 'SIGTERM');
  console.log(`Process ${pid} killed successfully`);
} catch (err) {
  if (err.code === 'ESRCH') {
    console.log(`Process ${pid} already terminated`);
  } else {
    throw err; // Re-throw unexpected errors
  }
}

This is especially important in cleanup handlers and shutdown sequences where processes may have already exited.

3Handle child process exit events properly

Listen to the 'exit' event on child processes to track their lifecycle and avoid killing already-exited processes:

javascript
const { spawn } = require('child_process');

const child = spawn('node', ['script.js']);
let childExited = false;

child.on('exit', (code, signal) => {
  childExited = true;
  console.log(`Child process exited with code ${code}`);
});

// Later in cleanup
function cleanup() {
  if (!childExited) {
    try {
      child.kill('SIGTERM');
    } catch (err) {
      if (err.code !== 'ESRCH') throw err;
    }
  }
}

Tracking process state prevents race conditions where the process exits between event loop ticks.

4Use AbortController for modern process management

Node.js 15+ supports AbortController for more reliable process cancellation:

javascript
const { spawn } = require('child_process');

const controller = new AbortController();
const { signal } = controller;

const child = spawn('node', ['long-running.js'], { signal });

// Abort cleanly without worrying about ESRCH
setTimeout(() => {
  controller.abort(); // Handles termination safely
}, 5000);

child.on('error', (err) => {
  if (err.name === 'AbortError') {
    console.log('Process aborted successfully');
  }
});

This modern API handles edge cases internally and prevents ESRCH errors from surfacing.

Advanced notes

Windows-specific behavior: On Windows, process.kill() throws ESRCH when attempting to kill process groups using negative PIDs (e.g., process.kill(-pid)). This works on Unix-like systems but is unsupported on Windows. Use the tree-kill package for cross-platform process tree termination.

Shell wrapper PIDs: When spawning child processes with { shell: true }, the PID returned by Node.js refers to the shell wrapper (sh, cmd.exe), not the actual command. The child process may continue running even after killing the shell. Use { shell: false } when possible or use process tree killing libraries.

PM2 and cluster mode: In cluster mode with PM2 or Node.js cluster module, workers may exit independently, causing the master process to encounter ESRCH when broadcasting kill signals. Implement worker tracking and remove PIDs from the pool immediately upon exit events.

Process.kill() signal 0: Sending signal 0 (process.kill(pid, 0)) is the standard POSIX way to test process existence. It's atomic and reliable but requires appropriate permissions. On Windows, it may return false positives for processes owned by other users.

Race conditions in tests: This error frequently appears in test suites that rapidly spawn and kill processes. Add small delays or use Promise-based process managers that properly await process termination before continuing.

Frequently asked questions

What does "ESRCH" mean?
kill() on a process ID that doesn't exist in the system. js's process management API and indicates that the system cannot find a process with the given PID. The error can occur with both the parent process and child processes spawned via the child_process module.
How do I fix "ESRCH"?
This error occurs when attempting to kill a process that no longer exists or never existed. The ESRCH code indicates that the specified process ID (PID) cannot be found in the system.
What causes "ESRCH"?
Process has already exited before the kill signal was sent (race condition) Incorrect PID value passed to process.kill() or subprocess.kill() On Windows, attempting to kill a shell wrapper instead of the actual process
Is the "ESRCH" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading