tiny-worker is a lightweight JavaScript library that provides a Web Worker-like API for Node.js environments, allowing developers to offload CPU-intensive tasks to separate threads, similar to the browser's `Worker` interface. It leverages Node.js's `child_process.fork()` module internally, offering a familiar `onmessage`, `postMessage`, and `terminate` API. The current stable version is 2.3.0, with its release cadence historically driven by feature enhancements and bug resolutions rather than a fixed schedule. A key differentiator is its flexibility in module loading within worker scripts, supporting Node.js `require()` by default and offering an `esm: true` option to enable ES6 `import`/`export` syntax, catering to diverse project setups. It is specifically designed for server-side use cases to improve application responsiveness by preventing the main event loop from being blocked by computationally intensive operations.
npm install tiny-workerVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create a worker from an inline function, send a message to it, receive a processed response, and ensure the worker is gracefully terminated upon completion or main process shutdown.
Implement `process.on('SIGTERM', ...)` and `process.on('SIGINT', ...)` listeners in your main script to call `worker.terminate()` for all active workers before `process.exit(0);`.Use `Worker.setRange(min, max)` in the main process to configure the automatic debug port range. For full manual control, pass `{ noDebugRedirection: true, execArgv: ['--inspect=PORT'] }` in the worker options, specifying a unique port.When creating a worker from a file, pass `{ esm: true }` in the worker options object: `new Worker('worker.js', [], { esm: true });`.Always refer to `self.onmessage` and `self.postMessage` within worker scripts. Be mindful of which global objects are available and their behavior compared to the main Node.js thread.
Add `process.on('SIGINT', () => { worker.terminate(); process.exit(0); });` (and `SIGTERM`) to your main application to ensure workers are shut down cleanly on process termination.Rewrite the worker script to use ES Module `import`/`export` syntax, or remove the `{ esm: true }` option from the `Worker` constructor if CommonJS is intended.When creating the worker from a file, pass `{ esm: true }` in the worker options: `new Worker('worker.js', [], { esm: true });`.Configure debug port redirection using `Worker.setRange(min, max)` in the main process. Alternatively, explicitly set `execArgv` with `--inspect=PORT` and `noDebugRedirection: true` in worker options to assign a unique, non-conflicting port.
No dependency data recorded yet.