Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-6443

instanceof checks don't work when mongodb and bson are used together in ESM project

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: BSON
    • 1

      I've recently started migrating my backend projects to ESM and encountered an issue where `instance instanceof Class` on bson objects don't work, if the `instance` comes from `bson` module and the `Class` from `mongodb` (or vice versa).

       

      The reason I end up with instances from one lib and Class from the other is that I use `bson` on it's own is some shared modules which contain Schema used for validation of unknown values, and then `mongodb`  in services which need to connect to mongo. The shared modules are also used on the frontend so I obviously don't want to pull in entire `mongodb` into the bundle. 

       

      I believe the root cause is that `mongodb` package is built as `cjs`, so when it re-exports `bson` Classes they are pulled from the `cjs` version of bson package. At the same time, since `bson` package is dual (containing both `mjs` and `cjs`) when it's imported from an ESM module, the msj version is returned. 

      I guess the solution would be to publish mongodb as dual as well, so that in ESM projects only the mjs versions of both packages would be used. 

            Assignee:
            Unassigned Unassigned
            Reporter:
            adrian.gierakowski@deversifi.com Adrian Gierakowski
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: