Skip to content

Error: Module parse failed with top-level await enabled #23021

@MaurizioCasciano

Description

@MaurizioCasciano

🐞 Bug report

Command (mark with an x)

  • build

Is this a regression?

No

Description

Angular Module build fails during parsing when using a top-level await.

🔬 Minimal Reproduction

@NgModule({
  declarations: [
  ],
  imports: [
    CommonModule,
    MyRoutingModule,
    MyApiModule
  ],
  providers: [
    {
      provide: Configuration,
      useFactory: await myAPIConfigFactory,
    },
  ]
})
export class MyModule {}

export function myAPIConfigFactory(): Promise<Configuration> {
  // The configuration is build via another service/observable
  return Promise.resolve(new Configuration());
}

🔥 Exception or Error


./my-app/my.module.ts:30:18 - Error: Module parse failed: Cannot use keyword 'await' outside an async function (30:18)
File was processed with these loaders:
 * ./node_modules/@angular-devkit/build-angular/src/babel/webpack-loader.js
 * ./node_modules/@ngtools/webpack/src/ivy/index.js
You may need an additional loader to handle the result of these loaders.
|       provide: Configuration,
|       useExisting: false,
>       useFactory: await myAPIConfigFactory
|     }]

🌍 Your Environment





     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 13.3.3
Node: 16.13.1
Package Manager: npm 8.1.2
OS: linux x64

Angular: 12.2.13
... animations, cdk, common, compiler, compiler-cli, core, forms
... language-service, material, platform-browser
... platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1202.13
@angular-devkit/build-angular   12.2.13
@angular-devkit/core            12.2.13
@angular-devkit/schematics      12.2.13
@angular/flex-layout            12.0.0-beta.35
@schematics/angular             12.2.13
rxjs                            6.6.7
typescript                      4.3.5

Anything else relevant?

The ECMEScript proposal is at Stage 4, so the proposal specification is completely ready and included within the next edition.
https://github.com/tc39/proposal-top-level-await

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions