ERR_WORKER_PATH
Node.jsERRORNotableWorkerHIGH confidence

Worker script path is invalid

Production Risk

Low — caught immediately at startup; always use __dirname or import.meta.url.

What this means

Thrown when the path provided to new Worker() is not a valid absolute path, file: URL, or a path that Node.js can resolve. Worker scripts must be provided as absolute paths or resolvable URLs; relative paths are not accepted.

Why it happens
  1. 1Passing a relative path like "./worker.js" instead of an absolute path
  2. 2Providing an invalid or non-existent file path
  3. 3Using a path with unsupported URL scheme
How to reproduce

Triggered when the Worker constructor validates the provided script path.

trigger — this will error
trigger — this will error
const { Worker } = require('worker_threads');
new Worker('./worker.js'); // relative path — throws ERR_WORKER_PATH

expected output

Error [ERR_WORKER_PATH]: The worker script filename must be an absolute path or a file URL

Fix 1

Use __dirname to construct an absolute path

WHEN In CommonJS modules

Use __dirname to construct an absolute path
const { Worker } = require('worker_threads');
const path = require('path');
const w = new Worker(path.join(__dirname, 'worker.js'));

Why this works

path.join(__dirname, ...) produces an absolute path that satisfies the Worker constructor validation.

Fix 2

Use import.meta.url to construct a file URL

WHEN In ES modules

Use import.meta.url to construct a file URL
import { Worker } from 'worker_threads';
import { fileURLToPath } from 'url';
const w = new Worker(new URL('./worker.js', import.meta.url));

Why this works

Using import.meta.url as a base produces a valid absolute file URL for the Worker constructor.

Code examples
Triggerjs
const { Worker } = require('worker_threads');
new Worker('./worker.js'); // relative path — throws ERR_WORKER_PATH  // this triggers ERR_WORKER_PATH
Handle in try/catchjs
try {
  // operation that may throw ERR_WORKER_PATH
  riskyOperation()
} catch (err) {
  if (err.code === 'ERR_WORKER_PATH') {
    console.error('ERR_WORKER_PATH:', err.message)
  } else {
    throw err
  }
}
Defensive pattern to avoid itjs
const { Worker } = require('worker_threads')
const path = require('path')
const w = new Worker(path.join(__dirname, 'worker.js'))
What not to do

Pass relative paths to the Worker constructor

Worker paths must be absolute; relative paths are rejected because the resolution context is ambiguous.

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