Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Cannot read private member from an object whose class did not declare it

I'm having some issues when I use a Proxy with the puppeteer library.

This is the class definition

const puppeteer = require("puppeteer");

class CustomPage {
  static async build() {
    const browser = await puppeteer.launch({ headless: false });
    const page = await browser.newPage();
    const customPage = new CustomPage(page);
    const superPage = new Proxy(customPage, {
      get: function (target, property) {
        //pay attention to the order between browser and page because of an issue
        return customPage[property] || browser[property] || page[property];
      }
    });
    console.log("superPage in CustomPage class definition", superPage);
    return superPage;
  }
  constructor(page) {
    this.page = page;
  }
}

module.exports = CustomPage;

And this is the error that I get when I run it

 TypeError: Cannot read private member from an object whose class did not declare it

       7 | beforeEach(async () => {
       8 |   page = await Page.build();
    >  9 |   await page.goto("localhost:3000");
         |              ^
      10 | });

Could you please help me with that?

like image 236
Pablo Alaniz Avatar asked Feb 27 '26 00:02

Pablo Alaniz


2 Answers

Looks like goto function calls some properties that declared as private like this #someProperty. Proxies and private properties cannot work together.

Here is the issue about that: https://github.com/tc39/proposal-class-fields/issues/106

In short, you have to abandon the idea of using one of them: proxies or private properties. In your case, since private properties are part of Puppeteer, you'll have to abandon proxy. I think it can be replaced with a wrapper.

like image 136
Ilham Khabibullin Avatar answered Feb 28 '26 12:02

Ilham Khabibullin


If anyone would prefer to not have to abandon the proxy I came up with a way that works(presumably for most cases can't speak for all).

{
  get(target, prop, receiver) {
    return (params) => {
      return target[prop](params);
    }
  }
}

Maybe this is what he meant by "some wrapper"? Not sure. It's been working for me so far.

like image 39
perpetual-light Avatar answered Feb 28 '26 12:02

perpetual-light



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!