ScreenshotNeo

BlogHow-to

Use Puppeteer’s Default Browser Context

Learn how Puppeteer’s default browser context works, when to use it, and why you must not close it. See runnable examples and when to create an isolated context.

By the ScreenshotNeo team4 October 20266 min read

Short answer: Call browser.defaultBrowserContext() to get Puppeteer’s default BrowserContext. Use it when pages should share the browser’s default session. You can create pages with context.newPage() or use browser.newPage(), which creates a page in the default context. Do not call close() on the default context: Puppeteer’s API reference says it cannot be closed. To isolate a session, create a separate context with browser.createBrowserContext() and close that context when you are done. [Puppeteer API: defaultBrowserContext()]

Get and use the default browser context

The accessor is synchronous: it returns the browser’s default BrowserContext directly, without await. This complete ES module example launches Puppeteer, opens a page through the context, navigates to a URL, and closes the browser in a finally block so the browser process is cleaned up even if navigation fails.

import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();

try {
  const context = browser.defaultBrowserContext();
  const page = await context.newPage();

  await page.goto('https://example.com', {
    waitUntil: 'domcontentloaded',
  });

  console.log(await page.title());
} finally {
  // Close the browser, not its default context.
  await browser.close();
}

Install Puppeteer in a project with npm install puppeteer and run the example in an environment that supports its bundled browser. The example uses top-level await, supported in Node.js ES modules. In a CommonJS file, place the body inside an async function and invoke it.

Use the browser shortcut for a simple page

If you do not need to name the context explicitly, browser.newPage() is the shorter form. Puppeteer documents that this creates a page in the default browser context. [Puppeteer API: Browser]

const page = await browser.newPage();
await page.goto('https://example.com');

Use browser.defaultBrowserContext() when the context itself matters—for example, when calling context-level methods or making page ownership clear in code that also creates isolated contexts. Both approaches use the default context in the documented API.

What the default context means

A browser launched or connected to through Puppeteer has a default context. A context groups pages under a browser session. Browser contexts do not share cookies or local storage; Puppeteer’s context creation reference also specifies that a newly created context does not share cookies or cache with other contexts. [Puppeteer API: BrowserContext] [Puppeteer API: createBrowserContext()]

The default context is the browser’s initial shared context. Pages opened with browser.newPage() belong to it. A popup opened with window.open belongs to the same context as the page that opened it. [Puppeteer API: BrowserContext]

Question Default context New context
How do I get one? browser.defaultBrowserContext() await browser.createBrowserContext()
How do I open a page in it? browser.newPage() or context.newPage() context.newPage()
Does it share browser storage with other contexts? It is the browser’s default session. Puppeteer documents separate cookies and cache; contexts do not share cookies or local storage.
Can I close the context? No. The default context cannot be closed. Yes. Closing it closes its pages.

In Puppeteer’s documented Chrome behavior, non-default contexts are incognito. The default context may also be incognito if Chrome is launched with --incognito. This is a Chrome-specific note in the reference, not a general claim about every browser. [Puppeteer API: BrowserContext]

When to use a separate browser context

Use the default context when one browser session is enough—for example, a single automated flow, or pages that should use the same session. Create a separate context when tasks need independent cookies, local storage, or cache, such as running unrelated sessions side by side. A page opened by a popup stays in the context of its opener, so create the intended context before opening the page that will spawn popups.

import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();

try {
  const context = await browser.createBrowserContext();
  try {
    const page = await context.newPage();
    await page.goto('https://example.com');
    console.log(await page.title());
  } finally {
    // Closing this created context also closes its pages.
    await context.close();
  }
} finally {
  await browser.close();
}

Do not use browser.defaultBrowserContext() when you need a context you can dispose of independently: Puppeteer forbids closing the default context. Close the context you created, or close the whole browser when the browser session is finished. [defaultBrowserContext()] [Puppeteer guide: Browser management]

Common mistakes and troubleshooting

Symptom Cause Fix
An error occurs when closing the default context The default context cannot be closed. Remove context.close() for the default context. Use await browser.close() to end the browser, or create a separate context if you need to close only one session.
Two tasks see the same session data Both pages are in the default context, which is shared. Create a context for each isolated session with await browser.createBrowserContext(), then create each page through its own context.
A popup appears in an unexpected session Popups inherit the context of the page that opened them. Open the parent page in the intended context before triggering the popup.
browser.defaultBrowserContext is not a function The value called browser may not be a Puppeteer Browser instance, or the installed Puppeteer version/API differs from the documentation used. Check how the browser was launched or connected and inspect the installed Puppeteer version. Consult the API reference matching that version; the cited references here show Puppeteer 25.11.0 or 25.12.0.
browser.newPage() works, but context-level code does not The shortcut creates a page but does not assign a context variable in your code. Call const context = browser.defaultBrowserContext() before using context-level methods.
Browser processes remain after an exception The browser was not closed after the task failed. Put await browser.close() in a finally block, as in the examples.

Performance, reliability, and cost notes

Choose contexts based on session ownership and isolation requirements. A separate context gives storage separation; it does not remove the need to manage browser and page lifecycles. Close created contexts when their work is complete and close the browser when the overall run ends. The cited Puppeteer references do not provide a performance benchmark or pricing figure, so do not assume that creating more contexts has a specific speed or cost impact; measure it in your own workload and hosting environment.

For repeatable automation, make page creation explicit: use context.newPage() for the session that owns the page, and use browser.newPage() when the default session is intended. Keep cleanup in finally blocks so a navigation or script error does not skip resource cleanup.

Or skip the browser setup

If your goal is a screenshot rather than browser automation, ScreenshotNeo can return an image or PDF with one GET request. See the ScreenshotNeo API documentation.

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

r = requests.get(
    "https://api.screenshotneo.com/v1/shot",
    params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"},
    timeout=90,
)
open("shot.webp", "wb").write(r.content)
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
if (!res.ok) throw new Error(`Screenshot request failed: ${res.status}`);
await Bun.write('shot.webp', res);

ScreenshotNeo accepts cookie and consent banners like a visitor, then removes more than 60 known consent platforms, newsletter popups, and chat widgets before the screenshot; each step can be turned off. Bot checks and CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing, and response headers report the page verdict and billing status. Its MCP server gives AI agents tools for screenshots, page information, and PDF capture. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 shots. Sign up free for 1,000 screenshots a month, with no card required.

FAQ

Is defaultBrowserContext() asynchronous?

No. It returns the context synchronously, so call it without await.

Does browser.newPage() use the default context?

Yes. Puppeteer documents that browser-level newPage() creates a page in the default browser context.

Can I close the default context and keep the browser running?

No. The default context cannot be closed. Close a separately created context to end only that context’s pages, or close the browser to end the browser session.

Do contexts isolate every aspect of browser state?

The cited references specifically document separation for cookies and local storage, and the context creation method explicitly calls out cookies and cache. Consult the API documentation for the Puppeteer version and browser you use when relying on other state boundaries.

References