ScreenshotNeo

BlogHow-to

How to Check the Type of a Puppeteer Console Message

Use Puppeteer’s page console event and `msg.type()` to identify each browser console message. See runnable examples, supported values, and troubleshooting tips.

By the ScreenshotNeo team4 October 20264 min read

To check a Puppeteer console message’s type, listen for the page’s console event and call type() on the ConsoleMessage supplied to the handler:

page.on('console', msg => {
  console.log(msg.type());
});

msg.type() returns the message category as a string, such as error, warn, or log. The event provides the message object; use its other methods when you need its text, arguments, or source information. See the official ConsoleMessage class reference and type() method reference.

Runnable example

Install Puppeteer in a Node.js project with npm install puppeteer. Save the following as console-types.js and run it with node console-types.js. The handler is registered before navigation so it can observe console messages emitted while the page loads.

const puppeteer = require('puppeteer');

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

    page.on('console', msg => {
      const type = msg.type();
      const text = msg.text();
      console.log(`[${type}] ${text}`);

      if (type === 'error') {
        console.error('Page console error:', text);
      }
    });

    await page.goto('https://example.com', { waitUntil: 'domcontentloaded' });
  } finally {
    await browser.close();
  }
})().catch(error => {
  console.error(error);
  process.exitCode = 1;
});

The listener handles console messages emitted by the page. It does not replace handling navigation failures or uncaught page errors; those are separate signals and should be wired separately if your monitoring needs them.

Filter by type and inspect message details

Use type() for the category and text() for a rendered text representation. For example, collect only errors:

page.on('console', msg => {
  if (msg.type() !== 'error') return;
  console.error(msg.text());
});

The ConsoleMessage API also exposes:

  • args() for the arguments passed to the console call.
  • location() for source location information.
  • stackTrace() for stack trace information.
  • text() for the message’s rendered text.

These methods answer different questions. Do not infer the category from the text when type() provides it directly. Use the object Puppeteer emits rather than constructing a ConsoleMessage yourself; the class reference marks its constructor as internal.

Supported console message types

The documented ConsoleMessageType union in Puppeteer 25.3.0 includes these strings:

Type string Typical use in a filter
log Ordinary console output
debug, info, verbose Diagnostic or informational output
warn, error Warnings and errors
dir, dirxml, table Structured console display methods
trace, assert, clear Trace, assertion, and console clearing calls
startGroup, startGroupCollapsed, endGroup Console group boundaries
profile, profileEnd, count, timeEnd Profiling, counter, and timer calls

The reference is versioned API documentation, and the available union may change. Check the type reference that matches the Puppeteer version installed in your project before relying on exhaustive handling.

Common mistakes

Symptom Cause Fix
You are logging message text but need its category. text() and type() provide different information. Call msg.type() for the category; use msg.text() for display.
Your handler does not receive ConsoleMessage objects. You attached a listener to the wrong event or object. Listen to page.on('console', handler).
Your code handles only log, warn, and error. The documented type union has additional values, including table, assert, and verbose. Use a default branch or account for the types relevant to your application.
You are trying to instantiate ConsoleMessage. The constructor is internal. Inspect instances delivered by Puppeteer’s page console event.
You see no messages during navigation. The listener may have been registered after navigation or the page emitted no console messages. Register the listener before goto() and verify the page actually calls a console method.

Performance and reliability notes

The type check is a local inspection of each event’s message object. Keep handlers lightweight when a page may emit many messages: avoid synchronous heavy work in the callback, and avoid retaining argument objects or full messages longer than needed. If storing logs, decide how to bound or filter them so a noisy page cannot grow memory without limit.

Register listeners before the action that may generate messages, and ensure cleanup such as closing the browser runs even when navigation throws. Compare against documented literal strings and keep a fallback branch so new or less common categories do not silently break your logging logic.

Or skip the browser setup

If your goal is to capture a page rather than inspect its console, ScreenshotNeo is a website screenshot API and MCP server. One GET request returns an image or PDF; see the API docs for options.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

ScreenshotNeo removes cookie banners, newsletter popups, and chat widgets 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.

Get 1,000 free screenshots a month—sign up with no card.

FAQ

Does type() return an enum?

It returns a ConsoleMessageType string. Compare it with documented string values such as 'error'.

Should I use type() or text() to find errors?

Use type() to identify messages categorized as errors. Use text() to read or display the message content.

Where can I confirm the available strings?

Check the ConsoleMessageType reference for the version of Puppeteer in your project; the cited reference documents version 25.3.0.