I've already tried everything mentioned in Error: Evaluation Failed: ReferenceError: util is not defined and How to pass required module object to puppeteer page.evaluate. Specifically, I've tried converting url.js using browserify (I've also tried converting url.js and punycode.js together), and I've added the corresponding script (bundle.js) to the page environment.
I'm trying to use the url module inside page.evaluate() in puppeteer. Here's a very simple example to show the error:
const puppeteer = require('puppeteer');
puppeteer.launch({dumpio: true}).then(async browser => {
const page = await browser.newPage();
const response = await page.goto('https://www.google.com');
await page.waitFor(5000);
const pageUrl = page.url();
await page.addScriptTag({path: 'bundle.js'});
await page.evaluate(pageUrl => {
const anchors = Array.from(document.querySelectorAll('a'));
for (let anchor of anchors) {
const href = anchor.getAttribute('href');
let hrefUrl;
try {
hrefUrl = new URL(href);
} catch (e) {
hrefUrl = new URL(href, pageUrl);
}
console.log(url.format(hrefUrl, {fragment: false}));
}
}, pageUrl);
await page.close();
await browser.close();
});
This example generates the following error:
(node:23667) UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: url is not defined at pageUrl (puppeteer_evaluation_script:11:19) at ExecutionContext.evaluateHandle (/home/webb/node_modules/puppeteer/lib/ExecutionContext.js:97:13) at at process._tickCallback (internal/process/next_tick.js:188:7)
What else do I need to do to get the url module recognized?