Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: page.waitForResponse results in "Buffer is not defined" error when reading response #18

Closed
apeacock1991 opened this issue Jul 21, 2023 · 11 comments
Labels
bug Something isn't working

Comments

@apeacock1991
Copy link

Bug description

Steps to reproduce the problem:

const [res] = await Promise.all([
            page.waitForResponse(res => res.url().includes('something') && res.request().method().toUpperCase() != "OPTIONS"),
            page.goto('https://example.com', {waitUntil: "domcontentloaded"}),
        ]);

console.log(await res?.buffer())

You'll need to adjust the URL/matcher, but when you call res.buffer() (or .text() or .json()) it raises a Buffer is not defined error.

I tested the same code on the core Puppeteer library and it worked fine.

Puppeteer version

0.0.4

Node.js version

16

npm version

9.5.1

What operating system are you seeing the problem on?

macOS

Relevant log output

No response

@apeacock1991 apeacock1991 added the bug Something isn't working label Jul 21, 2023
@lucaswalter
Copy link

I wanted to share I am getting a very similar error on my worker which was previously working from a few days ago where I take a screenshot of my own site.

            browser = await puppeteer.launch(env.OG_IMAGE_BROWSER);
            page = await browser.newPage();
            page.setViewport({ width: 1920, height: 1080, deviceScaleFactor: 2 });

            // Open Game Page
            await page.goto(`https://steambase.io/games/${appId}`);
            await page.waitForSelector('#app-stats-line-chart.loaded');

            // Capture Screenshot
            const img = await page.screenshot({
                type: 'png'
            });

This is the error I get when viewing the CF worker logs:

    {
      "message": [
        "ReferenceError: Buffer is not defined"
      ],
      "level": "error",
      "timestamp": 1689949193979
    }

@sizovs
Copy link

sizovs commented Jul 29, 2023

Same here.

@bhanderivaibhav
Copy link

mine is failing at initializing browser.

const browser = await puppeteer.launch(env.MYBROWSER);

image

@AlwaysAwake
Copy link

Same here.

@denu5
Copy link

denu5 commented Aug 4, 2023

update your dependencies incl. node to node 18 lts and try to add: compatibility_flags = [ "nodejs_compat" ]

similar:

#15 (comment)

@sizovs
Copy link

sizovs commented Aug 5, 2023

@denu5 Thank you. I tried all Node versions from 16 to 19 (with nodejs_compact flag enabled), but it didn't help.

@cahmetcan
Copy link

I have the same problem even though i enabled nodejs_compact flag

@roelmagdaleno
Copy link

I had the same problem when initializing the browser and I found a comment in Cloudflare's Discord with this solution:

import { Buffer } from 'node:buffer';
import puppeteer from "@cloudflare/puppeteer";

globalThis.Buffer = Buffer;

Basically, import the Buffer before puppeteer and register globally, so the library can be access from anywhere.

Hope that helps.

@meddulla
Copy link
Collaborator

meddulla commented Aug 16, 2023

@bhanderivaibhav That error can be due to a mismatch between the variable name defined in wrangler.toml and the name used in the worker code itself. So, in wrangler.toml:

browser = { binding = "MY_BROWSER", type = "browser" }

and in the worker

export default {
	async fetch(request, env, ctx) {
		const browser = await puppeteer.launch(env.MY_BROWSER); // MUST equal the binding name defined in wrangler 
                 //....
        }
}

Also check #15 (comment)

@meddulla
Copy link
Collaborator

meddulla commented Aug 16, 2023

@apeacock1991 Can you try with the latest versions of wrangler (3.5.1) and cloudflare/puppeteer (0.0.5)? Can reproduce that error with version 0.0.4 but not with 0.0.5 version. Other things to check out for in wrangler.toml:

compatibility_date = "2023-05-12"

compatibility_flags = ["nodejs_compat"]

@sizovs
Copy link

sizovs commented Aug 17, 2023

The solution by @roelmagdaleno fixed the problem for me. Thank you!

@meddulla meddulla closed this as completed Sep 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

9 participants