Free tools Windows power users keep installed
One-click scans. No signup required.
Use Puppeteer’s worker.evaluate() to run JavaScript inside a dedicated Web Worker. First identify the right worker—usually by listening for the page’s workercreated event before the app starts it, or by checking page.workers() if it is already running. page.evaluate() runs in the page’s main context, not in the worker.
Run code in a worker created during page startup
Register the listener before navigation or the interaction that starts the worker. Otherwise a quickly created worker could be missed.
As an Amazon Associate I earn from qualifying purchases.
import puppeteer from 'puppeteer';
const browser = await puppeteer.launch();
try {
const page = await browser.newPage();
const workerCreated = new Promise(resolve => {
page.once('workercreated', resolve);
});
await page.goto('https://example.com');
const worker = await workerCreated;
console.log('Worker URL:', worker.url());
const result = await worker.evaluate(() => {
// This function runs in the Worker, not the page.
return self.location.href;
});
console.log(result);
} finally {
await browser.close();
}
The promise is set up before page.goto(), so it can receive the event if the page creates its worker during startup. If your app starts the worker only after a click or another action, set up the listener first, perform that action, and then await the promise.
Recommended Free Tools
The example uses Puppeteer’s documented workercreated event and WebWorker API. See the WebWorker reference and WebWorker.url().
#1 Best Overall
Select the intended worker
A page can have multiple workers. Do not assume the first worker created is the one you want. Log or inspect each worker’s URL and select the one belonging to the feature under test.
If the worker already exists
Use page.workers() to list the active dedicated WebWorkers, then match the target by URL:
const workers = page.workers();
console.log(workers.map(worker => worker.url()));
const worker = workers.find(worker => worker.url().includes('/worker.js'));
if (!worker) {
throw new Error('Target worker was not found');
}
const result = await worker.evaluate(() => self.location.href);
console.log(result);
Change the URL test to fit your application. The Page.workers() reference documents the active-worker list; it includes dedicated WebWorkers, not ServiceWorkers.
Rank #2
If the worker starts after an interaction
const workerCreated = new Promise(resolve => {
page.once('workercreated', resolve);
});
await page.click('#start-worker');
const worker = await workerCreated;
const result = await worker.evaluate(() => self.location.href);
Use a selector and action that actually start the worker in your application. If that action can create several workers, listen for events and select by URL rather than resolving on the first event.
Pass data in and return serializable results
Puppeteer serializes the callback passed to evaluate() and executes it in the browser’s worker context. It does not carry over Node.js variables or helper functions from the surrounding lexical scope. Pass values as arguments and keep the required logic inside the callback.
const factor = 7;
const result = await worker.evaluate(value => value * 6, factor);
console.log(result); // 42
Prefer returning primitives or JSON-like objects. Complex objects may be truncated or returned as empty objects during protocol serialization. If you need to keep an in-context reference rather than transfer a value, use evaluateHandle(). See Puppeteer’s JavaScript execution guide and WebWorker.evaluate() reference.
Wait for worker state that changes later
worker.evaluate() awaits a promise returned by the callback. For a condition that becomes true after the evaluation completes, use worker.waitForFunction():
await worker.evaluate(() => {
self.answer = 42;
});
await worker.waitForFunction(() => self.answer === 42, { timeout: 5_000 });
Choose a timeout appropriate to the operation. The worker API documents polling, timeout, and abort-signal options; check the reference for the signature supported by your installed Puppeteer version: WebWorker.waitForFunction().
Understand which Puppeteer method runs where
| Method or API | Execution target | Use it for |
|---|---|---|
page.evaluate() |
The page’s JavaScript context | Running or inspecting page code, not code inside a worker. Reference |
worker.evaluate() |
The selected dedicated WebWorker | Executing code in that worker. Reference |
page.workers() |
Returns active dedicated WebWorkers | Finding a worker that has already started; it does not list ServiceWorkers. Reference |
page.evaluateOnNewDocument() |
A newly created page document before its scripts execute | Installing page-context code early; it is not the method for evaluating in a worker. Reference |
Troubleshoot worker evaluation
The worker-created promise never resolves
- Confirm the page actually starts a dedicated WebWorker after the listener is registered.
- If the app starts it only after a user action, perform that action after registering the listener.
- If it already exists, inspect
page.workers()instead of waiting for a creation event that has already happened. - Check whether the feature uses a ServiceWorker;
page.workers()lists dedicated WebWorkers, not ServiceWorkers.
The wrong worker receives the code
Inspect worker.url() and select by a URL pattern meaningful to your application. A page may create more than one worker, and the first one is not necessarily the target.
Rank #4
The callback cannot see a Node.js variable
That variable is outside the serialized callback’s browser context. Pass it as an explicit argument, as in the factor example, and define any helper logic inside the callback.
The result is empty or incomplete
Return a primitive or JSON-like value for serialization. For a complex object that must remain referenced in the browser context, use evaluateHandle().
A wait times out
Check that the worker condition can become true and that the code which changes it runs in the same worker. Adjust the timeout for the task and consult the installed-version API signature for polling and abort-signal options.
Best Value
Check your installed Puppeteer version
Puppeteer’s online API pages can display different documentation-version labels: the references relevant here surfaced labels from 25.5.0 through 25.12.0, while the JavaScript execution guide is labeled Next. These labels are not proof of the package version installed in your project or of when an API was introduced. Verify the local package’s types and the current official reference before relying on an exact signature. The project’s Puppeteer and browser versions are not specified here.
Or skip the browser setup
If your goal is to capture a page rather than execute code inside its worker, ScreenshotNeo offers a website screenshot API. A single GET request can return a screenshot or PDF; it does not replace worker.evaluate() for running code in a Web Worker.
For example, save a screenshot of a URL with cURL:
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
See the ScreenshotNeo API documentation for setup and options. ScreenshotNeo removes cookie banners, newsletter popups, and chat widgets before capture; bot checks, blank pages, and failed loads are never billed. Its MCP server lets AI agents take screenshots. The Free plan includes 1,000 shots per month with no card, and paid plans start at $5 for 3,000 shots. Learn about ScreenshotNeo or sign up free for 1,000 screenshots a month with no card.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Frequently Asked Questions
Does Puppeteer’s page.workers() include ServiceWorkers?
No. It lists dedicated WebWorkers, not ServiceWorkers.
Quick Recap
Can I use worker.evaluate() to access a Node.js variable directly?
No. Pass the value as an argument to the callback.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




