Is it possible to use @cloudflare/puppeteer
for local development of Cloudflare workers?
index.js (same as official example):
import puppeteer from "@cloudflare/puppeteer";
export default {
async fetch(request, env, ctx) {
const browser = await puppeteer.launch(env.MYBROWSER);
const page = await browser.newPage();
await page.goto("https://example.com");
const metrics = await page.metrics();
await browser.close();
return Response.json(metrics);
},
};
wrangler.toml:
#:schema node_modules/wrangler/config-schema.json
name = "my-first-worker"
main = "src/index.js"
compatibility_date = "2024-04-05"
compatibility_flags = ["nodejs_compat"]
browser = { binding = "MYBROWSER" }
Running this with wrangler dev
and then calling the worker throws the following error:
[wrangler:err] TypeError: Cannot read properties of undefined (reading 'fetch')
at PuppeteerWorkers.launch (file:///home/duranda/my-first-worker/node_modules/@cloudflare/puppeteer/src/puppeteer-core.ts:107:32)
at Object.fetch (file:///home/duranda/my-first-worker/src/index.js:15:35)
at fetchDispatcher (file:///home/duranda/my-first-worker/.wrangler/tmp/bundle-3eSyZw/middleware-loader.entry.ts:56:17)
at __facade_invokeChain__ (file:///home/duranda/my-first-worker/node_modules/wrangler/templates/middleware/common.ts:53:9)
at Object.next (file:///home/duranda/my-first-worker/node_modules/wrangler/templates/middleware/common.ts:50:11)
at jsonError (file:///home/duranda/my-first-worker/node_modules/wrangler/templates/middleware/middleware-miniflare3-json-error.ts:22:30)
at __facade_invokeChain__ (file:///home/duranda/my-first-worker/node_modules/wrangler/templates/middleware/common.ts:53:9)
at Object.next (file:///home/duranda/my-first-worker/node_modules/wrangler/templates/middleware/common.ts:50:11)
at drainBody (file:///home/duranda/my-first-worker/node_modules/wrangler/templates/middleware/middleware-ensure-req-body-drained.ts:5:30)
at __facade_invokeChain__ (file:///home/duranda/my-first-worker/node_modules/wrangler/templates/middleware/common.ts:53:9)
Their docs say that Browser Rendering is not supported in Local Mode and, hence, --remote
option is required