puppeteer: Using esm (ES6 modules) causes failure.

Steps to reproduce

Tell us about your environment:

  • Puppeteer version: 6.4.1
  • Platform / OS version: Linux latitude 4.15.0-36-generic #39-Ubuntu SMP Mon Sep 24 16:19:09 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
  • URLs (if applicable):
  • Node.js version: 10.10.0

What steps will reproduce the problem?

Please include code that reproduces the issue.

  1. copy example code into a new file example.js
  2. replace const puppeteer = require('puppeteer') with ìmport puppeteer from 'puppeteer'
  3. run with node -r esm example.js

What is the expected result? Empty prompt, example.png in script folder.

What happens instead?

node -r esm demo.js 
(node:11730) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'on' of undefined
    at Function.addEventListener (../node_modules/puppeteer/lib/helper.js:165:13)
    at new NavigatorWatcher (../node_modules/puppeteer/lib/FrameManager.js:1146:14)
    at FrameManager.navigateFrame (../node_modules/puppeteer/lib/FrameManager.js:75:21)
    at Frame.goto (../node_modules/puppeteer/lib/FrameManager.js:404:37)
    at Page.goto (../node_modules/puppeteer/lib/Page.js:579:49)
    at require (../demo.js:6:14)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:11730) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 11
  • Comments: 20 (6 by maintainers)

Commits related to this issue

Most upvoted comments

Yes, likely within a week or so.

Well that week-or-so turned into a bit longer with the US Thanksgiving holiday. I should release before I start more aggressive perf work. I’ll ping back this thread when it’s released.

BTW, I had this issue with puppeteer 1.9.0, had to roll back to 1.8.0 which didn’t produce the error. Then accidentally updated it 1.11.0 and it just worked! Here’s my setup: https://github.com/sergiitk/pagerbeauty/blob/122aca70976db520487906ac670b98d1177b275c/Dockerfile-test-acceptance

Chromium 71 / Ava 1.0.1 / Puppetter 1.11.0 / Docker / Node 10.15.0-alpine

Thanks @aslushnikov!

I narrowed it down to a faulty Symbol.hasInstance helper on our end (https://github.com/standard-things/esm/commit/111328e69eafc25829508329e1b1d853951f7dc0).

I’m open to digging into it from the esm side of things.

@jdalton awesome, reopening.

Do you have any idea what could have changed between v1.8.0 from v1.9.0?

Thanks to @damianobarbati input, I’ve debugged it down to d547b9d. The core issue seems to be that an instance of Connection class satisfies the instanceof check against CDPSession class.

Here’s a small snippet from Connection class (check out the console.log statements): image

@jdalton any ideas what’s going on here?

@jdalton is the release done?

@aslushnikov after digging into the code I noted that this does not seem to not be related to ESM but to this line into into Connection.js: https://github.com/GoogleChrome/puppeteer/blob/d547b9d24a803c0adf923b3a821ae5f43e79bb53/lib/Connection.js#L51 which is called here into FrameManager.js: https://github.com/GoogleChrome/puppeteer/blob/c237947b324338ece670b4f4e879880265ec6965/lib/FrameManager.js#L1146

  • connection._connection is undefined
  • connection instanceof CDPSession is true
  • loop starts and connection._connection which is undefined is assigned to connection
  • undefined is not an instance of CDPSession, “while” loop stops and undefined is returned
  • helper.addEventListener(Connection.fromSession(client), ..., ...) attempt to attach event on undefined and the error is fired

Replacing the following into https://github.com/GoogleChrome/puppeteer/blob/d547b9d24a803c0adf923b3a821ae5f43e79bb53/lib/Connection.js#L50 solves the issue

while (connection instanceof CDPSession && connection._connection)

But I’m not sure whether that makes “logic” sense.

I’m using ava, which controls Node and compilation. This is the relevant issue:

https://github.com/avajs/ava/issues/1810

The error we’re seeing:

Rejected promise returned by test. Reason:

  TypeError {
    message: 'Cannot read property \'on\' of undefined',
  }

  Function.addEventListener (node_modules/puppeteer/lib/helper.js:165:13)
  new NavigatorWatcher (node_modules/puppeteer/lib/FrameManager.js:1146:14)
  FrameManager.navigateFrame (node_modules/puppeteer/lib/FrameManager.js:75:21)
  Frame.goto (node_modules/puppeteer/lib/FrameManager.js:404:37)
  Page.goto (node_modules/puppeteer/lib/Page.js:579:49)
  goto (test/helpers/puppeteer-page.js:17:24)
  visit (test/integration/search.test.js:17:9)