mongodb-js / charts-embed-sdk

The easiest way to embed MongoDB Charts visualisations into your web app
https://docs.mongodb.com/charts/master/embedding-charts-sdk/
Apache License 2.0
43 stars 31 forks source link

Error when bundling code in version 3.3.1 #98

Open ValYouW opened 3 months ago

ValYouW commented 3 months ago

After upgrading to version 3.3.1 I started to get the following error when building my code (using esbuild):

The top-level await in "node_modules/bson/lib/bson.mjs" is here:

    node_modules/bson/lib/bson.mjs:111:26:
      111 │ const nodejsRandomBytes = await (async () => {
          ╵                           ~~~~~

It seems the error comes from bson lib that was updated in version 3.3.1 to ^5.4.0 (used to be 4.5.2 in 3.2.1)

mongo-J commented 3 months ago

Hi @ValYouW, thank you for raising this issue and sorry for the inconvenience. For esbuild, have you tried to alias the bson to use cjs as below. I've done a quick test in my local environment it seems to resolve this issue. You should also be able to define this from an esconfig file. We had a similar issue during our testing in which we've added an alias to the package.json which fixed this problem for a few other bundling tools, but looks like esbuild doesn't not pick up this from package.json. We are also trying to workout with the bson library team to if there can be a better solution to this.

--alias:bson="./node_modules/bson/lib/bson.cjs"
jyriaganitsh commented 3 months ago

I had similar issue. I downgraded back to v.3.0.0 and seems it helped to resolve the issue for now.

peters commented 3 months ago

Same here. Had to downgrade to v3.2.1.

troyha commented 3 months ago

Same here. Had to downgrade to v3.2.1.

Anton-Plagemann commented 2 months ago

Got the same issue with nuxt (vite) :( Had to use https://github.com/Menci/vite-plugin-top-level-await to make it work. Any chance for a fix?

dgwight commented 2 months ago

To use version 3.3.1 of mongodb-js/charts-embed-sdk, set the overrides or resolves of bson in the package.json to force it to use that version

For npm

  "overrides": {
    "bson": "^4.5.2"
  }

For yarn

  "resolves": {
    "bson": "^4.5.2"
  }