nil3217
nil3217
CDCloudflare Developers
Created by Zersya on 5/24/2024 in #workers-help
Cannot read properties of undefined (reading 'fetch') with Node
I'm facing the same error. I cannot make the getting started guide work — here's the keybinding I use:
#:schema node_modules/wrangler/config-schema.json
name = "round-unit-1901"
main = "src/index.js"
compatibility_date = "2024-05-24"
compatibility_flags = ["nodejs_compat"]

browser = { binding = "MY_BROWSER" }
kv_namespaces = [
{ binding = "BROWSER_KV_DEMO", id = "...", preview_id = "..." }
]
#:schema node_modules/wrangler/config-schema.json
name = "round-unit-1901"
main = "src/index.js"
compatibility_date = "2024-05-24"
compatibility_flags = ["nodejs_compat"]

browser = { binding = "MY_BROWSER" }
kv_namespaces = [
{ binding = "BROWSER_KV_DEMO", id = "...", preview_id = "..." }
]
and here' the index.js file
import puppeteer from '@cloudflare/puppeteer';

export default {
async fetch(request, env) {
const { searchParams } = new URL(request.url);
let url = searchParams.get('url');
let img;
if (url) {
url = new URL(url).toString(); // normalize
console.log(env);
img = await env.BROWSER_KV_DEMO.get(url, { type: 'arrayBuffer' });
if (img === null) {
const browser = await puppeteer.launch(env.MYBROWSER);
const page = await browser.newPage();
await page.goto(url);
img = await page.screenshot();
await env.BROWSER_KV_DEMO.put(url, img, {
expirationTtl: 60 * 60 * 24,
});
await browser.close();
}
return new Response(img, {
headers: {
'content-type': 'image/jpeg',
},
});
} else {
return new Response('Please add an ?url=https://example.com/ parameter');
}
},
};
import puppeteer from '@cloudflare/puppeteer';

export default {
async fetch(request, env) {
const { searchParams } = new URL(request.url);
let url = searchParams.get('url');
let img;
if (url) {
url = new URL(url).toString(); // normalize
console.log(env);
img = await env.BROWSER_KV_DEMO.get(url, { type: 'arrayBuffer' });
if (img === null) {
const browser = await puppeteer.launch(env.MYBROWSER);
const page = await browser.newPage();
await page.goto(url);
img = await page.screenshot();
await env.BROWSER_KV_DEMO.put(url, img, {
expirationTtl: 60 * 60 * 24,
});
await browser.close();
}
return new Response(img, {
headers: {
'content-type': 'image/jpeg',
},
});
} else {
return new Response('Please add an ?url=https://example.com/ parameter');
}
},
};
9 replies