Worker script path is invalid
Production Risk
Low — caught immediately at startup; always use __dirname or import.meta.url.
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.
- 1Passing a relative path like "./worker.js" instead of an absolute path
- 2Providing an invalid or non-existent file path
- 3Using a path with unsupported URL scheme
Triggered when the Worker constructor validates the provided script path.
const { Worker } = require('worker_threads');
new Worker('./worker.js'); // relative path — throws ERR_WORKER_PATHexpected 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
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
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.
const { Worker } = require('worker_threads');
new Worker('./worker.js'); // relative path — throws ERR_WORKER_PATH // this triggers ERR_WORKER_PATHtry {
// 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
}
}const { Worker } = require('worker_threads')
const path = require('path')
const w = new Worker(path.join(__dirname, 'worker.js'))✕ Pass relative paths to the Worker constructor
Worker paths must be absolute; relative paths are rejected because the resolution context is ambiguous.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev