Error upon importing BSON library saying "The top-level-await experiment is not enabled"

1k views Asked by At

I get a top-level-await error upon when importing BSON:

./node_modules/bson/lib/bson.mjs - Error: Module parse failed: The top-level-await experiment is not enabled (set experiments.topLevelAwait: true to enabled it)
File was processed with these loaders:
 * ./node_modules/@angular-devkit/build-angular/src/tools/babel/webpack-loader.js
 * ./node_modules/source-map-loader/dist/cjs.js
You may need an additional loader to handle the result of these loaders.
Error: The top-level-await experiment is not enabled (set experiments.topLevelAwait: true to enabled it)

Module parse failed: The top-level-await experiment is not enabled (set experiments.topLevelAwait: true to enabled it)

I'm importing the library inside my component like so:

import { ObjectId } from 'bson';

Been searching everywhere but can't find an answer.

1

There are 1 answers

2
Gérôme Grignon On BEST ANSWER

You can update your angular builder to esbuild:

"architect": {
  "build": {
    "builder": "@angular-devkit/build-angular:browser-esbuild",

Still experimental in Angular, it'll be tagged as stable with the Angular 17 release.