Worker thread initialisation failed
Production Risk
Worker pool exhaustion if init failures are not detected and retried.
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.
- 1Worker script has a syntax error or throws during import/require
- 2System is out of memory and cannot allocate a new V8 isolate
- 3Worker resource limits (e.g. maxOldGenerationSizeMb) are set too low
Triggered when the worker thread environment cannot be successfully initialised.
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
// 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
const w = new Worker('./worker.js', {
resourceLimits: {
maxOldGenerationSizeMb: 256,
maxYoungGenerationSizeMb: 64,
},
});Why this works
Higher resource limits prevent premature OOM failures during worker startup.
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_FAILEDtry {
// 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
}
}// Validate inputs before calling the operation
function safe_err_worker_init_failed(...args) {
// validate args here
return performOperation(...args)
}✕ Leave worker startup errors unhandled
Uncaught worker init errors propagate to the main thread as unhandled worker errors.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev