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

Listen for the page’s console event, then call msg.location() on the ConsoleMessage object. It returns a structured object with the message’s URL, line number, and column number when Puppeteer knows them. For additional stack locations, use msg.stackTrace().

Get a console message’s URL, line, and column

Browser-side console.* output does not automatically appear in Node.js. Puppeteer exposes it through page.on('console'); the event listener receives a ConsoleMessage. Call location() to retrieve its source coordinates:

page.on('console', msg => {
  const location = msg.location();
  console.log({
    text: msg.text(),
    url: location.url,
    lineNumber: location.lineNumber,
    columnNumber: location.columnNumber,
  });
});

location() returns an object, not a formatted string. Its documented fields are url, lineNumber, and columnNumber. Each field is optional, so a value may be undefined when unavailable. Line and column numbers are zero-based; add one only when presenting them to people who expect one-based numbering, not when passing the values to tools expecting Puppeteer’s original coordinates. See the location() API and ConsoleMessageLocation interface.

Log missing values defensively

If you want the log to make an absent URL explicit without implying that Puppeteer found one, provide a display fallback:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
page.on('console', msg => {
  const {url, lineNumber, columnNumber} = msg.location();
  console.log(msg.text(), {
    url: url ?? '(unknown URL)',
    lineNumber,
    columnNumber,
  });
});

The fallback changes only what is printed; the original API data remains optional.

Use stackTrace() when one location is not enough

msg.location() gives the concise location associated with the message. If you need the array of locations on the message’s stack, call msg.stackTrace() instead:

Rank #2
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option
page.on('console', msg => {
  console.log(msg.text(), msg.stackTrace());
});

The method returns an array of locations, but the API does not promise that it will be non-empty for every message. Check for an empty array before assuming a frame is available. For compact structured logging, use location(); for investigating multiple stack locations, inspect stackTrace(). Both may provide incomplete information. See the stackTrace() API.

Keep browser and Node.js debugging distinct

The page event is the bridge for browser-side console messages: attach the listener to the Puppeteer Page you are debugging. If you need to inspect browser execution interactively rather than only capture logs, Puppeteer’s debugging guide describes launching with devtools: true and using a debugger statement.

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

The API pages consulted span Puppeteer documentation versions 25.1.0, 25.3.0, and 25.12.0. Check the API reference for the version installed in your project before relying on version-specific behavior.

Troubleshoot missing source coordinates

  • URL or coordinates are undefined: treat them as unavailable metadata. The interface marks these fields optional; do not interpret their absence alone as a Puppeteer error.
  • The stack array is empty: handle it as a valid case. The documented return type is an array, not a guarantee of at least one location.
  • You cannot see browser logs in Node.js: verify that a page.on('console', ...) listener is attached to the relevant page and that the output is generated by browser-side code.
  • The location is not the authored source you expected: the API documentation does not guarantee that a reported URL resolves to authored code rather than a generated resource, nor does it specify behavior for every browser, protocol, source-map configuration, or Puppeteer release.
  • You need to step through execution: use the browser debugger approach in the official debugging guide in addition to collecting console events.

Or skip the browser setup

If your goal is a screenshot rather than capturing Puppeteer console coordinates, ScreenshotNeo offers a one-request screenshot API. For example, with cURL:

Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
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 docs for request options. Cookie banners, popups, and chat widgets are removed before the shot; bot checks, blank pages, and failed loads are never billed. Its MCP server lets AI agents take screenshots. The Free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000. Sign up for free.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Frequently Asked Questions

Does msg.location() return a string?

No. It returns an object with optional url, lineNumber, and columnNumber fields.

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

How do I get a stack trace from a Puppeteer console message?

Call msg.stackTrace() in the page console event listener; check for an empty array before using its locations.

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.