October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober 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 Detach a Puppeteer CDPSession

Use await client.detach() to detach a Puppeteer CDP session. Learn what becomes unusable afterward, how to handle cleanup in finally, and how this differs from closing a page or browser.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Call and await client.detach() on the CDP session when you are finished with it. This detaches that protocol session from its target; it does not mean “close the page” or “close the browser.”

Detach a session

Assuming page is an existing Puppeteer Page, create a session and detach it when its work is done:

As an Amazon Associate I earn from qualifying purchases.

const client = await page.createCDPSession();

// Use client.send(...) and client.on(...) while the session is needed.
await client.detach();

Puppeteer 25.12.0 documents Page.createCDPSession() as returning a Promise<CDPSession>. The CDPSession.detach() reference defines the method as Promise<void>; awaiting it makes the cleanup step explicit.

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.

What detaching changes

A CDPSession is a connection for sending Chrome DevTools Protocol commands and listening for protocol events. Use send() to send commands and on() to subscribe to events. Puppeteer’s API documentation states: “Once detached, the cdpSession object won’t emit any events and can’t be used to send messages.” See the send method reference and CDPSession class reference.

Do not reuse the detached object

After detaching, do not rely on that session to send further CDP commands or receive events. If you need CDP access later, create a session again with page.createCDPSession() and use the new session object.

Detaching is not page or browser closure

The method documentation describes detaching the CDPSession from its target. It does not say that detach() closes the Puppeteer Page or browser. Treat protocol-session cleanup separately from any page- or browser-lifecycle action your application needs.

Ensure cleanup when work can fail

If session work may throw and you still want to attempt detachment, put the work in a try block and await detachment in finally:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const client = await page.createCDPSession();

try {
  await client.send('Runtime.enable');
  // Perform the CDP work that needs this session.
} finally {
  await client.detach();
}

This pattern makes cleanup part of the control flow. The API reference does not state that detachment is idempotent, so do not assume repeated calls are guaranteed to be harmless.

Optional state and event observation

The CDPSession API documents a readonly detached boolean property, and Puppeteer documents a sessiondetached event in its CDPSessionEvents interface. They are observation surfaces; the cited references do not specify when the property changes or how the event is ordered relative to the detach promise resolving. Avoid making cleanup logic depend on an undocumented timing assumption.

Version note

The method and page-session creation references cited here are for Puppeteer 25.12.0. The event interface reference is for 25.9.0, while the class reference is labeled Next. If you use another installed release, check the API documentation for that version before relying on version-specific behavior.

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 simply to capture a website rather than use raw CDP, ScreenshotNeo provides a website screenshot API and MCP server. One GET request can return an image or PDF without you managing a Puppeteer browser session. The example below saves a WebP response; consult the ScreenshotNeo API documentation for request options.

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://stripe.com 
  -o shot.webp
  • Cookie and consent banners, newsletter popups, and chat widgets are removed before capture; each cleanup step can be turned off.
  • Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed; responses indicate the page verdict and billing status in headers.
  • An MCP server lets AI agents use screenshot tools, including take_screenshot, get_page_info, and capture_pdf.
  • The Free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 screenshots.

Sign up for ScreenshotNeo free and get 1,000 screenshots a month with no card.

Frequently Asked Questions

Does detach() return a value?

The Puppeteer 25.12.0 API signature is Promise<void>.

Can I keep listening for CDP events after detaching?

No. Puppeteer documents that the detached session no longer emits events.

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.