Esm: Top-level await doesn't work with ts-node typescript files.

Created on 22 Aug 2018  路  6Comments  路  Source: standard-things/esm

.esmrc: { "await": true }

topAwait.js|ts:console.log(await (new Promise((resolve) => resolve('ok'))));

Above code works if it's run with both node -r esm topAwait.js or ts-node -r esm topAwait.js.

if run with ts-node -r esm topAwait.ts, It fails with topAwait.ts(1,13): error TS2304: Cannot find name 'await'.

question

Most helpful comment

Well spotted, I managed to get it to work with following:

export TS_NODE_IGNORE_DIAGNOSTICS=1308,2304
node -r esm -r ts-node/register topAwait.ts

the order of requires is crucial. For the same reason ts-node -r esm topAwait.ts fails.

Is might be possible to set that global ignore or something similar for TS1308 from esm if top-level await is enabled before ts-node/register is activated since it's very specific to this. Unfortunately TS2304 is more difficult since Cannot find name 'await' is the same error for all unrecognised symbols.

All 6 comments

Hi @Pokute!

For ts-node you'll need to do some more ts-node config. See this comment.

Well spotted, I managed to get it to work with following:

export TS_NODE_IGNORE_DIAGNOSTICS=1308,2304
node -r esm -r ts-node/register topAwait.ts

the order of requires is crucial. For the same reason ts-node -r esm topAwait.ts fails.

Is might be possible to set that global ignore or something similar for TS1308 from esm if top-level await is enabled before ts-node/register is activated since it's very specific to this. Unfortunately TS2304 is more difficult since Cannot find name 'await' is the same error for all unrecognised symbols.

Did you set the TS target to es2017?

Target doesn't matter, as long as it's at least es5.

Based on the comment from the linked issue:

Set target to "es2017" or higher. Otherwise an awaiter is generated by TypeScript and await is rewritten to yield

I'm not a TS user so I don't know if this still holds true.

it still does

Was this page helpful?
0 / 5 - 0 ratings