Puppeteer: Default BrowserContext cannot be closed!
close is called on the default BrowserContext.
close is called on the default BrowserContext.
Error message
Default BrowserContext cannot be closed!
How this error happens
The default context belongs to the browser lifecycle. It cannot be closed as if it were an independently created isolated context.
The snippets use a running Puppeteer browser named browser.
const context = browser.defaultBrowserContext();
await context.close();
How to fix
Create a separate context when your task needs context-level cleanup. Close the whole browser only when you own its lifecycle.
const context = await browser.createBrowserContext();
try {
const page = await context.newPage();
await page.setContent(
"<p>Isolated task</p>"
);
} finally {
await context.close();
}
Things to keep in mind
Closing a context closes all of its pages. Do not close a shared remote browser just to clear one task’s cookies.