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

Driver cannot find package.json in util.ts when bundled

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Blocker - P1 Blocker - P1
    • 4.0.0
    • Affects Version/s: 4.0.0
    • Component/s: Shell
    • None
    • Not Needed

      We cannot connect to anything using the driver when it's bundled because it will fail with:

      MongoServerSelectionError: The 'driver.version' field must be a string in the client metadata document 
      

      Because it appears the compiled driver cannot find the package.json file here: https://github.com/mongodb/node-mongodb-native/blob/4f9d4ceafdcca01d49e6eeb286a9c27ddf485969/src/utils.ts#L953-L955.

      For mongosh, we use parcel, for which __dirname is not accessible.

      A related problem also happens when we try to use the driver with the VSCode extension, in which we get 

      Uncaught Error: ENOENT: no such file or directory, open '/package.json'
      

      because for webpack __dirname is accessible, but you cannot use read file sync and expect the file to be included in the bundle.

       

      Would it be possible to find a solution that doesn't require getting the version using a file read? A straightforward solution would be in a prebuild step, a version.ts file could be generated that would be usable by bundlers like webpack or parcel. An alternative is using

      require('../package.json'

       

            Assignee:
            anna.herlihy@mongodb.com Anna Herlihy (Inactive)
            Reporter:
            anna.herlihy@mongodb.com Anna Herlihy (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: