inline-worker is a JavaScript utility designed to create a universal Web Worker instance directly from a function. It abstracts away the boilerplate of setting up separate worker files, allowing developers to define worker logic inline within their main script. The package intelligently determines whether to instantiate a native `Worker` in browser environments or a custom `InlineWorker` implementation for Node.js, providing a consistent API across different JavaScript runtimes. The current stable version is 1.1.0, and the package has not seen updates in over eight years (as of April 2026), indicating a low release cadence. Its key differentiator is the inline definition and universal runtime support, making it convenient for small, self-contained worker tasks without the need for separate files or complex build steps. However, its age suggests potential compatibility issues with modern JavaScript features, module systems, and a lack of ongoing maintenance.
npm install inline-workerVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create an `InlineWorker` from a function, pass a shared `self` object for internal worker function testing, send messages to the worker, and receive responses.
For Node.js, use `const InlineWorker = require('inline-worker');`. In browser environments or ESM projects, ensure your bundler (e.g., Webpack, Rollup, Parcel) is configured to correctly handle CommonJS modules, or consider using a modern alternative that supports ESM natively.For new projects, consider using a more actively maintained Web Worker library, or implement native Web Workers directly. For existing projects, thoroughly vet its compatibility with your current stack and consider forking the repository if critical maintenance is required.
Create a `declarations.d.ts` file with `declare module 'inline-worker';` or provide more specific types if you understand the internal API, e.g., `declare module 'inline-worker' { class InlineWorker extends Worker { constructor(func: Function, self?: object); } export = InlineWorker; }`If shared state is not intended, avoid passing the `self` object or ensure that the worker logic does not mutate it in ways that would affect the main thread. If shared access is desired, clearly document the interaction points to prevent unintended side effects.
Ensure you are using the CommonJS `require` syntax: `const InlineWorker = require('inline-worker');`.Within the worker function, `postMessage` should be called directly, as it's a global function within the Web Worker scope. If you passed a `self` object to the constructor, ensure you're not inadvertently using a property on that `self` object named `postMessage` that shadows the global one. You can also explicitly use `self.postMessage()` where `self` refers to the worker's global scope.
Ensure the first argument to `new InlineWorker()` is a properly defined JavaScript function that encapsulates your worker's logic, as shown in the package examples.
No dependency data recorded yet.