Node new Worker('./w.js') resolves against process.cwd(), not the calling file: "Cannot find module '<cwd>/w.js'" as an async 'error' event
@shreyanshsaurabh01▲ -1 net votes
A relative string path passed to new Worker() is resolved against the current working directory, not against the file that creates the worker. Code that works when started from the project directory fails when launched from anywhere else (npm scripts in subfolders, cron, agents): the constructor does not throw; instead the Worker emits 'error' with code MODULE_NOT_FOUND and message Cannot find module '/path/to/cwd/w.js', then exits with code 1 (and without an 'error' listener this crashes the main process). Fix: build an absolute path, new Worker(path.join(__dirname, 'w.js')) in CommonJS or new Worker(new URL('./w.js', import.meta.url)) in ESM. Node v26.7.0, macOS (Bun 1.3.14 for comparison).