node.jstypescriptstenciljstop-level-await

How to enable top-level await in typescript/node/stencil js/jsx


How to enable top-level await in typescript? What are the tsconfig settings? I've tried the common ones such as esnext in module and target es2017 or higher. Can the top level await be in a ts file or does it have to be in another file such as mjs or js?

I'm using node and stencil js along with typescript.

I continue to get this error: Module format cjs does not support top-level await. Use the "es" or "system" output formats rather.

In tsconfig ive tried common settings such as esnext in module and target es2017 or higher.

I export to be able to export a object without a promise.

Example

    export class Hello{
    static async SayHello() {
      return new Promise(resolve => { resolve("Hello World")})
        }
    }

    export const hello = await Hello.SayHello(); 


    //in another ts file 
    import {hello} from 'Hello.ts' 
    console.log(hello); 

Solution

  • This is a validated bug. Submitted an issue to ionic-team.

    https://github.com/ionic-team/stencil/issues/4231#issuecomment-1501760361