October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Get the Source Location of a Puppeteer Console Message

Use Puppeteer’s page console event and call msg.location() to log a console message’s URL, line number, and column number, handling missing values safely.
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 an object with the message’s URL, line number, and column number when Puppeteer knows them; each field can be undefined.

Capture the URL, line, and column from a console event

Browser-side console.* output does not appear in Node.js automatically. Puppeteer exposes it through the page’s console event. Register a listener before the page runs the code whose output you want to inspect:

As an Amazon Associate I earn from qualifying purchases.

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  try {
    const page = await browser.newPage();

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

    await page.goto('https://example.com');
    // Trigger or wait for the page's console output here.
  } finally {
    await browser.close();
  }
})();

The essential call is msg.location(). The returned value is a structured object, not a formatted string. Its documented fields are url, lineNumber, and columnNumber. See the Puppeteer ConsoleMessage API reference and the ConsoleMessageLocation interface.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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

Handle missing metadata

The location fields are optional. If a value is unavailable, preserve that fact rather than logging it as a confirmed source coordinate. Line and column numbers are zero-based; add one only when presenting them to people in a one-based editor or display, not when passing the original values to tooling.

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

Choose between location() and stackTrace()

Use location() for the compact source coordinates associated with the message. Use stackTrace() when you need the array of locations on the message’s stack:

page.on('console', msg => {
  const frames = msg.stackTrace();
  console.log(msg.text(), frames);
});

The API describes stackTrace() as returning an array of locations, but does not guarantee that the array is populated for every message. Check it before relying on a frame:

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 => {
  const frames = msg.stackTrace();
  const firstFrame = frames[0];

  console.log({
    text: msg.text(),
    firstFrame: firstFrame ?? null,
  });
});

For everyday structured logs, the single optional location object is usually the simpler choice. For investigating additional stack locations, inspect the array and handle the case where it has no entries. See the stackTrace() API reference.

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

Tell browser output apart from Node.js output

The page.on('console') listener captures console messages emitted by the browser page. It does not make Node.js console output part of the page’s console stream. When diagnosing a problem, identify which side produced the message before tracing its source.

If you need to step through browser execution interactively, Puppeteer’s debugging guide describes launching with devtools: true and using a debugger statement.

Troubleshoot missing or unexpected locations

  • url, line, or column is undefined: Treat it as unavailable metadata. The API permits undefined fields; inspect stackTrace() for other useful locations, while allowing for an empty array.
  • No browser message reaches the listener: Confirm that the listener is attached to the relevant page before the browser code runs, and that the message is emitted by the page rather than Node.js.
  • The displayed line or column appears off by one: Puppeteer’s values are zero-based. Convert to one-based numbering only for human-facing display; keep the original numbers for tools that expect API values.
  • The URL is not the authored source you expected: A location identifies a resource location when available; the API documentation does not guarantee that it resolves to authored code rather than a generated resource.

Behavior is not fully specified in the cited API pages for every browser, protocol, source-map setup, or Puppeteer release. Check the reference for the version installed in your project before depending on version-specific behavior.

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
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 you also need a page image rather than console coordinates, ScreenshotNeo is a website screenshot API; it does not replace Puppeteer’s console-event listener or return a console message’s source location. One GET request can capture a page:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.webp

See the ScreenshotNeo API documentation for request options. Before a capture it accepts cookie or consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets; those steps can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers report the page verdict and billing status. Its MCP server provides screenshot tools for AI agents. The free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots.

Sign up for 1,000 free screenshots a month, with no card required.

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
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

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.