Puppeteer: Could not serialize referenced object
A referenced browser object cannot be serialized into a Node.js value.
A referenced browser object cannot be serialized into a Node.js value.
Error message
Could not serialize referenced object
How this error happens
jsonValue must bring a browser object back by value. A complex object such as the global window contains references that cannot be represented as a normal Node.js value.
The snippets use an open Puppeteer page named page.
const handle = await page.evaluateHandle(() => {
return window;
});
await handle.jsonValue();
How to fix
Extract only the fields you need inside the browser instead of serializing the entire object graph.
const details = await page.evaluate(() => {
return {
title: document.title,
url: location.href,
};
});
console.log(details);
Things to keep in mind
Dispose the handle after use. Whether a specific object fails depends on the browser’s serialization; the point is to transfer selected plain data rather than a global object.