question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

async function inside evaluate fails

See original GitHub issue

Code

await page.evaluate(async () => {
    console.log('1')
})

Error

(node:66796) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Evaluation failed: ReferenceError: fn is not defined
    at <anonymous>:1:26

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:15
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

88reactions
JoelEinbindercommented, Dec 28, 2017

Are you transforming your code with babel? The babel async function code isn’t compatible with Puppeteer. We call function.toString and send your code into Chromium, but babel messes with that and we end up sending an incomplete string. You can get around this by using template strings instead of functions.

await page.evaluate(`(async() => {
   console.log('1');
})()`);
63reactions
cancerberoSgxcommented, Oct 12, 2018

same issue, different error, using TypeScript (not babel). The solution for me was to instruct TypeScript to compile to latest ecma version so no pollyfills / artificial code is generated for async function support, in tsconfig.json file:

{
  "compilerOptions": {
    "target": "es2018"
  }
}

issue details

While the following works OK:

    let result = await page.evaluate( ()=>{
      return Promise.resolve('hello')
    })

the equivalent code using async functions throws the following error:

    let result = await page.evaluate(async ()=>{
      return 'hello'
    })

Error:

  Message:
    Error: Evaluation failed: ReferenceError: __awaiter is not defined
        at __puppeteer_evaluation_script__:1:8
  Stack:
    Error: Evaluation failed: ReferenceError: __awaiter is not defined
        at __puppeteer_evaluation_script__:1:8
        at ExecutionContext.evaluateHandle (/home/sg/git/autumn-leaves/imagemagick-browser/node_modules/puppeteer/lib/ExecutionContext.js:106:13)
        at process._tickCallback (internal/process/next_tick.js:68:7)
        at <Jasmine>
        at ExecutionContext.<anonymous> (/home/sg/git/autumn-leaves/imagemagick-browser/node_modules/puppeteer/lib/helper.js:144:27)
        at ExecutionContext.evaluate (/home/sg/git/autumn-leaves/imagemagick-browser/node_modules/puppeteer/lib/ExecutionContext.js:58:31)
        at ExecutionContext.<anonymous> (/home/sg/git/autumn-leaves/imagemagick-browser/node_modules/puppeteer/lib/helper.js:145:23)
        at Frame.evaluate (
Read more comments on GitHub >

github_iconTop Results From Across the Web

Puppeteer - Async function in evaluate method throws error
If I want to call async method in evaluate function, I get Error: Evaluation failed: [object Object] error. const puppeteer = require(" ...
Read more >
async inside evaluate function is not working #3312 - GitHub
I get the error below when I run this code, I have no idea why its not working. await page.evaluate(async () => {...
Read more >
await - JavaScript - MDN Web Docs
The await operator is used to wait for a Promise and get its fulfillment value. It can only be used inside an async...
Read more >
Dart: using `await` when evaluating async function results in ...
Dart: using `await` when evaluating async function results in compilation error ; Project, WebStorm ; Type, Bug ; State, Incomplete I ; Assignee,...
Read more >
Puppeteer Evaluation failed: ReferenceError: __awaiter is ...
So, to make the TypeScript compiler not transform async functions, you need to change the compilerOptions.target in tsconfig.json to at least ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found