ERR_WORKER_INIT_FAILED
Node.jsERRORNotableWorkerHIGH confidence

Worker thread initialisation failed

Production Risk

Worker pool exhaustion if init failures are not detected and retried.

What this means

Thrown when a worker thread fails to initialise. This can occur if the worker script throws an error during startup before entering the event loop, or if there is a resource problem such as being unable to create a V8 isolate.

Why it happens
  1. 1Worker script has a syntax error or throws during import/require
  2. 2System is out of memory and cannot allocate a new V8 isolate
  3. 3Worker resource limits (e.g. maxOldGenerationSizeMb) are set too low
How to reproduce

Triggered when the worker thread environment cannot be successfully initialised.

trigger — this will error
trigger — this will error
const { Worker } = require('worker_threads');
const w = new Worker('./bad-worker.js'); // bad-worker.js throws at startup
w.on('error', (err) => {
  console.error(err.code); // ERR_WORKER_INIT_FAILED
});

expected output

Error [ERR_WORKER_INIT_FAILED]: Worker initialization failure

Fix 1

Fix errors in the worker script startup code

WHEN When the worker script throws during initialisation

Fix errors in the worker script startup code
// bad-worker.js — avoid throwing at the top level
try {
  const config = JSON.parse(process.env.CONFIG || '{}');
  // ... use config
} catch (err) {
  process.exit(1); // exit cleanly instead of throwing
}

Why this works

Wrapping startup code in try/catch prevents uncaught exceptions from crashing the worker before it initialises.

Fix 2

Increase resource limits for workers

WHEN When workers are failing due to memory limits

Increase resource limits for workers
const w = new Worker('./worker.js', {
  resourceLimits: {
    maxOldGenerationSizeMb: 256,
    maxYoungGenerationSizeMb: 64,
  },
});

Why this works

Higher resource limits prevent premature OOM failures during worker startup.

Code examples
Triggerjs
const { Worker } = require('worker_threads');
const w = new Worker('./bad-worker.js'); // bad-worker.js throws at startup
w.on('error', (err) => {
  console.error(err.code); // ERR_WORKER_INIT_FAILED
});  // this triggers ERR_WORKER_INIT_FAILED
Handle in try/catchjs
try {
  // operation that may throw ERR_WORKER_INIT_FAILED
  riskyOperation()
} catch (err) {
  if (err.code === 'ERR_WORKER_INIT_FAILED') {
    console.error('ERR_WORKER_INIT_FAILED:', err.message)
  } else {
    throw err
  }
}
Defensive pattern to avoid itjs
// Validate inputs before calling the operation
function safe_err_worker_init_failed(...args) {
  // validate args here
  return performOperation(...args)
}
What not to do

Leave worker startup errors unhandled

Uncaught worker init errors propagate to the main thread as unhandled worker errors.

Same error in other languages
Sources
Official documentation ↗

Node.js Error Codes Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Node.js errors