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

Different instanceof results for commonjs and esmodule

    • Type: Icon: Question Question
    • Resolution: Duplicate
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • 1
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      I have tested the node.js mongodb driver 4.x, 5.x, 6.x and found some interesting results.

      When using commonjs, all versions have the same behavior. the class (e.g. Decimal128) imported from "mongodb" and "bson" are the same instance.

      When using es module, version 4.x has the same behavior as commonjs, but 5.x and 6.x have different behavior. The class imported from "mongodb" and "bson" are different instances. 

      I'm curious that is it intended?

      I'v tested it in node-20.10.0 and no packages installed other than the mognodb. So I'm pretty sure that there is only one version of bson package installed.

       

      // for es module (set "type": "module" in package.json)
      import assert from 'assert'
      import { Decimal128 as D1 } from 'mongodb'
      import { Decimal128 as D2 } from 'bson'

      // for commonjs
      const assert = require('assert')
      const { Decimal128: D1 } = require('mongodb')
      const { Decimal128: D2 } = require('bson')

      // commonjs: passed for 4.x, 5.x and 6.x
      // esmodule: passed for 4.x, failed for 5.x and 6.x
      assert(D1.fromString('1.234') instanceof D2)

            Assignee:
            neal.beeken@mongodb.com Neal Beeken
            Reporter:
            tzhuan@gmail.com Tz-Huan Huang
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: