-
Type: Bug
-
Resolution: Unresolved
-
Priority: Unknown
-
None
-
Affects Version/s: None
-
Component/s: BSON
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.
- depends on
-
NODE-6453 POC: Ship an es module formatted driver
- Backlog
- related to
-
NODE-5756 ObjectId instances created by bson and mongodb are not interchangeable
- Closed
-
NODE-5872 ObjectId.equals throws vague error two bson packages are loaded
- Closed
-
NODE-5873 js-bson - PR #643: fix: use Symbol.for('id') in ObjectId to fix errors when mixing cjs and mjs
- Closed
-
NODE-5796 Different instanceof results for commonjs and esmodule
- Closed