Have a javascript application which includes a shared module and a main module. Both modules include the same version of puppeteer
. A method in the shared module is responsible for creating the browser and handing off the page
instance to my main module to interact with it. This works fine, but I'm running into an issue trying to capture a TimeoutError in the main module.
I'm trying to do something like this:
try {
await page.waitForXPath("//")
} catch (error) {
if (error instanceof TimeoutError) {
continue;
} else {
throw error;
}
However, the error instanceof TimeoutError
never returns true.
Here is a minimal reproducible example.
Shared package.json
{
"name": "shared",
"version": "1.0.0",
"description": "",
"type": "module",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"puppeteer": "^20.7.2"
}
}
Shared main.js
import * as puppeteer from "puppeteer";
export function throwError() {
throw new puppeteer.TimeoutError("Timeout from shared");
}
Main package.json
{
"name": "main",
"version": "1.0.0",
"description": "",
"type": "module",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"puppeteer": "^20.7.2",
"shared": "^1.0.0"
}
}
Main main.js
import * as puppeteer from "puppeteer";
import * as shared from "shared";
try {
shared.throwError();
} catch (error) {
console.log(error instanceof puppeteer.TimeoutError);
}
I would expect this to print True
, but it does not. I suspect its because the puppeteer
imported by the main module is not the same puppeteer
imported by the shared module, even though they are the same version.
To run this locally, I've run npm link
in shared
project and then npm link shared
in the main project.
If I remove the puppeteer* modules from both the main/node_modules and main/node_modules/shared/node_modules directories and install it in the same directory as main and shared folders, the example works as expected since the same puppeteer modules are resolved for both.
It looks like that it's a bug from puppeteer itself, well described in this Github issue : https://github.com/puppeteer/puppeteer/issues/7545
You can update to the latest version of puppeteer, to see if it's fixed or You can use the following as a workaround (string comparison on the error's name)