Use Puppeteer’s page.exposeFunction(name, callback) to register a Node.js callback as a function on a page’s window. Browser-side code can call that function and await a Promise for the result. The callback runs in Node.js; page.evaluate() runs its function in the browser page.
How the Node-to-browser bridge works
Register the function from Node.js on the specific Puppeteer Page that needs it. Puppeteer makes it available as window[name]. When page code calls it, Puppeteer invokes the supplied callback in Node.js and returns a Promise that resolves to the callback’s result. If the callback itself returns a Promise, Puppeteer waits for it.
As an Amazon Associate I earn from qualifying purchases.
Puppeteer’s official Page.exposeFunction() API reference describes the method this way: “The method adds a function called name on the page’s window object.” The reference identifies Puppeteer 25.12.0.
Recommended Free Tools
Runnable example: expose a Node.js function
This example registers a Node.js function that calculates an MD5 digest, then calls it from page-context code. It follows the pattern in Puppeteer’s documentation; it is an example, not a separately tested result.
#1 Best Overall
const puppeteer = require('puppeteer');
const crypto = require('node:crypto');
(async () => {
const browser = await puppeteer.launch();
try {
const page = await browser.newPage();
// This callback runs in Node.js, not inside the browser page.
await page.exposeFunction('md5', (text) =>
crypto.createHash('md5').update(text).digest('hex')
);
// This function runs in the browser page. The exposed function is
// available on window and returns a Promise for the Node.js result.
const digest = await page.evaluate(async () => {
return await window.md5('PUPPETEER');
});
console.log(digest);
} finally {
await browser.close();
}
})();
The example uses CommonJS. In an ES module, import the dependencies with import puppeteer from 'puppeteer' and import crypto from 'node:crypto', then use the same registration and page-evaluation pattern. Install Puppeteer in your project and use API documentation matching the version installed there; the cited expose-function and evaluate references identify version 25.12.0.
What runs where: exposeFunction() versus evaluate()
| Call | Where its function runs | Purpose |
|---|---|---|
page.exposeFunction(name, callback) |
The registered callback runs in Puppeteer’s Node.js context; page code calls the exposed name on window. |
Make Node-side work callable from the page. |
page.evaluate(pageFunction, ...args) |
pageFunction runs in the page context. |
Run browser-side code and return its result to the Node.js caller. |
page.evaluate() is a convenient way to demonstrate or invoke the exposed function, but its callback is not a Node.js callback. Puppeteer waits if the evaluated page function returns a Promise. See the official Page.evaluate() API reference, which identifies Puppeteer 25.12.0.
Rank #2
Expose an asynchronous Node.js operation
The callback may return a Promise. For example, Puppeteer’s documentation demonstrates exposing a Node.js file-reading function backed by fs.readFile. Page code should await the exposed function so it receives the completed result:
await page.exposeFunction('readfile', (path) => {
return fs.readFile(path, 'utf8');
});
const contents = await page.evaluate(async () => {
return await window.readfile('/path/to/file');
});
Import fs in the Node.js module before using this snippet. Treat any path or other input supplied by page code as untrusted: an exposed callback gives page code a route to invoke Node-side behavior, so restrict what the callback accepts and does.
Rank #3
Page scope, navigation and removal
Register on the page that needs the function
A Puppeteer Browser can have multiple Page instances. Exposing a function on one page makes it available to that page’s window; register it on the page whose context will call it.
The function survives navigation
Puppeteer’s expose-function reference says exposed functions survive navigations. Register before the page-side code needs to call the function, and use the same Page instance.
Rank #4
Remove an exposed function when finished
Call page.removeExposedFunction(name) to remove a previously exposed name from the page’s window. Consult the Page.removeExposedFunction() reference for the installed Puppeteer version; the cited reference identifies version 25.3.0.
Troubleshooting
window.md5is missing: confirmpage.exposeFunction('md5', ...)completed on the samePagebefore page code calls it. Check spelling and letter case in both the exposed name and the browser call.- The callback cannot access a browser variable: the callback runs in Node.js, not in the page’s JavaScript context. Pass the needed value as an argument from page code, or use page-side logic inside
page.evaluate(). - The Node.js caller gets no expected result: await the exposed function in page code and return or otherwise use the result. The exposed call returns a Promise; asynchronous Node callbacks must finish before that Promise resolves.
- The function disappears after a page change: exposed functions are documented to survive navigations. Verify that navigation occurred on the same Puppeteer
Pageinstance and that the function was not removed withremoveExposedFunction().
Or skip the browser setup
If your goal is to capture a website rather than run custom Node.js logic in its page, ScreenshotNeo returns a screenshot or PDF from one GET request. For example, using 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 request options. Cookie banners, popups and chat widgets are removed before the shot; bot checks, blank pages and failed loads are never billed. An MCP server lets AI agents take screenshots. The Free plan includes 1,000 screenshots a month with no card, and paid plans start at $5 for 3,000. Sign up for free.
Frequently Asked Questions
Does page.evaluate() run its function in Node.js?
No. The function passed to page.evaluate() runs in the page context; its result is returned to the Node.js caller.
Can an exposed function return a Promise?
Yes. Puppeteer waits for a Promise returned by the Node.js callback, and page code receives a Promise for the result.
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallCrashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteWhich Puppeteer version should I use for these examples?
Use documentation matching your installed version. The cited exposeFunction() and evaluate() references identify Puppeteer 25.12.0; the cited removal reference identifies 25.3.0.
Quick Recap
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.




