Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Support top-level-await #997

Closed
swwind opened this issue Nov 5, 2020 · 32 comments
Closed

Support top-level-await #997

swwind opened this issue Nov 5, 2020 · 32 comments

Comments

@swwind
Copy link

swwind commented Nov 5, 2020

$ node -v
v14.13.1

$ cat index.js 
console.log(await Promise.resolve(233));

$ cat package.json 
{
  "type": "module"
}

$ node index.js
233

$ pkg index.js -t node14
> pkg@4.4.9
> Error! Unexpected token, expected "," (1:18)
  /home/swwind/Repositories/tmp/index.js
@Dunky13
Copy link

Dunky13 commented Dec 18, 2020

I have the same issue, top level await doesn't want to compile.

@github-actions
Copy link

This issue is stale because it has been open 90 days with no activity. Remove the stale label or comment or this will be closed in 5 days. To ignore this issue entirely you can add the no-stale label

@github-actions github-actions bot added the Stale label Mar 25, 2021
@github-actions
Copy link

This issue is now closed due to inactivity, you can of course reopen or reference this issue if you see fit.

@Dunky13
Copy link

Dunky13 commented Mar 31, 2021

Still an issue

@robertsLando
Copy link
Contributor

Does the issue persists on 4.5.1?

@Dunky13
Copy link

Dunky13 commented Apr 1, 2021

Yes,

4.5.1: > Error! 'await' is only allowed within async functions (45:12)

tsconfig.json
target: ESNext
module: ESNext
moduleResolution: node

package.json
type: module
"pkg": {
        "options": [
            "loader=ts-node/esm.mjs",
            "experimental-top-level-await",
            "harmony-top-level-await"
        ]
    },

@robertsLando
Copy link
Contributor

That's strange, @jesec any clue?

@Dunky13
Copy link

Dunky13 commented Apr 2, 2021

I will share a MVP example soon here

@jesec
Copy link
Contributor

jesec commented Apr 2, 2021

Top level await is not allowed in any of the current Node versions supported by pkg. This is expected behavior.

@robertsLando
Copy link
Contributor

robertsLando commented Apr 2, 2021

@jesec node 13.3+ should allow that, just needs a flag --harmony-top-level-await

@jesec
Copy link
Contributor

jesec commented Apr 2, 2021

Need --harmony-top-level-await and only allowed in ESM mode.

@robertsLando
Copy link
Contributor

I have edited my comment, btw I think that we already have versions that support it we should understand why it is not working, maybe node options are not correctly applied on start?

@jesec
Copy link
Contributor

jesec commented Apr 2, 2021

This should be treated as a feature request. We can’t support every experimental Node feature out there.

@robertsLando
Copy link
Contributor

I think we should add it here?

const activeBakes = bakes.filter((bake) => {

@Dunky13
Copy link

Dunky13 commented Apr 2, 2021

I've shared the (trunkated) settings I've tried to use. But even with harmony-top-level-await

@erossignon
Copy link
Contributor

erossignon commented Apr 5, 2021

// a.js
console.log(await Promise.resolve(42));

doesn't work with nodejs either the and this is the expected behavior. top level await is not supported yet in nodejs by default unless you use the --harmony-top-level-await flag)

$ node a.js
console.log(await Promise.resolve(42));
               ^^^^^

SyntaxError: missing ) after argument list

a simple workaround is to wrap the async call into an async function

(async ()=>{
  console.log(await Promise.resolve(42));
})();

@robertsLando
Copy link
Contributor

doesn't work with nodejs either and this is the expected behavior (top level await is not supported yet in nodejs)

Not correct, it is supported starting from nodejs 13.3+ but requires a flag, see my comment #997 (comment)

@Dunky13
Copy link

Dunky13 commented Apr 6, 2021

@robertsLando how should I run pkg then, I can't seem to get it to work.

@robertsLando
Copy link
Contributor

I'm not 100% sure but you may need to add that option on fabricator.js @Dunky13, I have linked you the line in my prev comment

@jesec
Copy link
Contributor

jesec commented Apr 6, 2021

Features are hidden behind flags for a reason.

@Dunky13
Copy link

Dunky13 commented Apr 6, 2021

That I understand, but as stated in #997 (comment) I tried enabling these flags in package.json

@Digicrat
Copy link

Digicrat commented Jun 2, 2021

A flag is not required when using await on an import statement, ie: "await import('node-fetch')".

This bug is preventing me from building an application that uses the above syntax to conditionally import a library in code meant to run in either browser or node. This is distinct from using await in general, which I didn't even know there was a flag to enable before seeing this thread.

Edit: There appear to be other issues as well causing 'Failed to make bytecode' when using ES6 module imports after temporarily disabling the above syntax.

@markwylde
Copy link

I get the same error:

Error! 'await' is only allowed within async functions

When using the node16 target, of which top level await is enabled by default.

@vsnthdev
Copy link

I get the same error:

Error! 'await' is only allowed within async functions

When using the node16 target, of which top level await is enabled by default.

Yup, I am also getting the exact same error.

@saostad
Copy link

saostad commented Aug 16, 2021

top-level await supported since 2020-08-11 in node js 14.8 without any flag as a default behavior.

@webhype
Copy link

webhype commented Sep 1, 2021

This is still an issue and no longer an experimental feature hidden behind a flag.

@ryanblock
Copy link

Having this issue as well.

@ksturner
Copy link

ksturner commented Oct 3, 2021

I'm having this issue as well.

@robertsLando
Copy link
Contributor

@jesec I think this could be fixed once we add support for es modules parsing

@saostad
Copy link

saostad commented Oct 5, 2021

@jesec I think this could be fixed once we add support for es modules parsing

is there a road map / ETA for es modules parsing?

@robertsLando
Copy link
Contributor

robertsLando commented Oct 6, 2021

I created a test here: #1323 and I tried to fix it but I have no idea about the changes needed to make the support of them.

The problem is that actually we are using npm resolve module to get modules paths but it doesn't support es modules yet: browserify/resolve#222

@elijaholmos
Copy link

I am also having this issue

@jesec jesec changed the title Compile Error when using top-level-await Support top-level-await Nov 10, 2021
@vercel vercel locked and limited conversation to collaborators Nov 10, 2021
@jesec jesec closed this as completed Nov 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests