`web-worker` is a JavaScript library providing a consistent, cross-platform Web Worker API that operates natively in both browser environments and Node.js. It achieves this by aliasing to the browser's native `Worker` constructor when bundled for the browser and implementing a web-compatible `Worker` atop Node.js's `worker_threads` module. The current stable version is 1.5.0, with a fairly active release cadence addressing bug fixes, compatibility improvements, and new features like Bun support and `importScripts()`. Key differentiators include its ability to make worker code compatible across environments, native support for Module Workers in Node 12.8+, use of DOM-style events (`Event.data`, `Event.type`), support for event handler properties (`worker.onmessage`), and acceptance of module, Blob, or Data URLs. It also emulates `WorkerGlobalScope` within the worker context, offering a familiar environment for developers.
npm install web-workerVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to create and communicate with a simple Web Worker, handling file creation and cleanup, suitable for both browser and Node.js environments.
Upgrade Node.js to version 16 or higher for full compatibility and official support.
Always use `new URL('./worker.js', import.meta.url)` to correctly resolve worker paths relative to the current module, ensuring consistent behavior across environments and preventing path resolution errors.Ensure `web-worker` is at least version 1.4.0, which includes compatibility fixes. If using mixed CJS/ESM environments, verify your build tooling properly handles module interop or consider standardizing on a consistent module type.
Upgrade `web-worker` to version 1.4.0 or higher, which includes a specific fix for this `ReferenceError` in shared worker contexts.
Instantiate the worker with `new Worker(new URL('./worker.js', import.meta.url))` to ensure the worker script's path is resolved relative to the current module, providing consistent behavior across environments.No dependency data recorded yet.