DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk4 min

How to Expose a Node.js Function to the Browser with Puppeteer

Expose a Node.js callback on a Puppeteer page’s window, call it from browser code, and understand page scope, navigation behavior and cleanup.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Troubleshooting

  • window.md5 is missing: confirm page.exposeFunction('md5', ...) completed on the same Page before 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 Page instance and that the function was not removed with removeExposedFunction().
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Which 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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.