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.

"Error: 'return' outside of function" with async/await

See original GitHub issue

Current Behavior

Hello! Thank you for making this awesome tool.

By the way, an error occurred while testing to set up the project using this tool.

const puppeteer = require('puppeteer')
;(async () => {
  const browser = await puppeteer.launch()
  const page = await browser.newPage()
  await page.goto('https://example.com')
  await page.screenshot({ path: 'example.png' })

  await browser.close()
})()

Error: 'return' outside of function (Note that you need plugins to import files that are not JavaScript)

at /Users/seungdols/dev/getGold/src/index.ts:3:24

1: const puppeteer = require('puppeteer')
2: ;(async () => {
3:   const browser = await puppeteer.launch()
                           ^
4:   const page = await browser.newPage()
5:   await page.goto('https://example.com')

Expected behavior

If you build using tsc, it is a code that builds normally.

Why this error message?

I can’t build a module of puppeteer example code.

I would like to know why the error occurs in a simple async/await code.

And I couldn’t understand exactly what the error message was saying, so I had to ask a question.

What exactly did I do wrong with this error message? I don’t know, can you change the message a little more kindly?

And can you guide me on how to solve this problem?

Your environment

OSX 10.15.6
Node 12.16.3

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
seungdolscommented, Sep 29, 2020

@agilgur5 Thank you for explaining in detail.

Actually, I tried to find this bug, but I still don’t understand the structure of the project, but you solved it.

Thanks to the kind explanation, it was an opportunity to understand the project more. It was really good for teaching.

thank you.😁

1reaction
agilgur5commented, Sep 20, 2020

async-to-promises has been replaced with babel-plugin-polyfill-regenerator in #795 and will be released in v0.14.0 soon. It will only pure polyfill generators for targets that need a polyfill according to your browserslistrc or preset-env targets.

For your specific purpose, since puppeteer runs in Node, I would recommend adding a .browserslistrc:

node 10
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using await outside of an async function - Stack Overflow
I don't see where the problem is with that (maybe you already updated the repo)? If you refer to the isInLobby().then( … countPlayer().then...
Read more >
How to use await outside of an async function in JavaScript
In this example, we will simply create a method and inside that method, we will return a promise which will actually use the...
Read more >
How to use await outside of an async function in JavaScript
There are multiple ways to use the `await` operator outside of an `async` function - use a Node.js version higher than `16.12.0`, load...
Read more >
"Error: 'return' outside of function" with async/await · Issue #869
I would like to know why the error occurs in a simple async/await code. And I couldn't understand exactly what the error message...
Read more >
Async and Await - LearnHowToProgram.com
An async function allows us to write asynchronous code as if it were synchronous. This can make our code more concise. And while...
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