Puppeteer: JSHandle is disposed!
The JSHandle has been disposed.
The JSHandle has been disposed.
Error message
JSHandle is disposed!
How this error happens
Disposing a handle releases its browser-side reference. Passing that handle into a later evaluation reuses a released reference.
The snippets use an open Puppeteer page named page.
const handle = await page.evaluateHandle(() => {
return {
count: 3,
};
});
await handle.dispose();
await page.evaluate(
(value) => value.count,
handle
);
How to fix
Use the handle before disposing it, and keep cleanup in a finally block.
const handle = await page.evaluateHandle(() => {
return {
count: 3,
};
});
try {
const count = await page.evaluate(
(value) => value.count,
handle
);
console.log(count);
} finally {
await handle.dispose();
}
Things to keep in mind
Navigation can also invalidate an otherwise undisposed handle. Recreate it in the new document.