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

Incorrect imports in ESM bundle

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • bson-4.2.0
    • Affects Version/s: bson-4.0.4
    • Component/s: js-bson
    • None

      I first created this as an issue on the public GitHub repository: https://github.com/mongodb/js-bson/issues/371.

      I believe the bson package's esm bundles are suffering from a bug originating in the rollup-plugin-commonjs dependency (now @rollup/plugin-commonjs - I verified that the bug is still present if upgrading to the latest version of this new package).

      I believe the root cause is that describe in the "External imports in the CommonJS plugin" sub-section of this issue on the Rollup.js repository:

      *� Bug 5:* For external imports, this plugin will always require the default import.

      Because of this, when "long" and "buffer" is imported via `require("long")` and `require("buffer")` in the various sourcefiles and gets translated to `import long from "long"` and `import buffer from "buffer"` by the commonjs plugin, it produces an invalid bundle, since "buffer" does provide a `default` export.

       


      This results in a downstream issue when trying to bundle this package into my own IIFE bundle, using Rollup.

      I've attached is some code to reproduce the issue that I'm seeing.

      Output from running `npx rollup -c`

      > rollup -cmain.js → bundle.js...
      [!] Error: 'default' is not exported by node_modules/long/src/long.js, imported by node_modules/bson/dist/bson.esm.js
      https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module
      node_modules/bson/dist/bson.esm.js (1:7)
      1: import long from 'long';
                ^
      2: import buffer from 'buffer';
      Error: 'default' is not exported by node_modules/long/src/long.js, imported by node_modules/bson/dist/bson.esm.js
          at error (/Users/kraenhansen/Projects/bson-bug/node_modules/rollup/dist/shared/rollup.js:5171:30)
          at Module.error (/Users/kraenhansen/Projects/bson-bug/node_modules/rollup/dist/shared/rollup.js:9603:16)
          at handleMissingExport (/Users/kraenhansen/Projects/bson-bug/node_modules/rollup/dist/shared/rollup.js:9525:28)
          at Module.traceVariable (/Users/kraenhansen/Projects/bson-bug/node_modules/rollup/dist/shared/rollup.js:9986:24)
          at ModuleScope.findVariable (/Users/kraenhansen/Projects/bson-bug/node_modules/rollup/dist/shared/rollup.js:8549:39)
          at MemberExpression.bind (/Users/kraenhansen/Projects/bson-bug/node_modules/rollup/dist/shared/rollup.js:6324:49)
          at AssignmentExpression.bind (/Users/kraenhansen/Projects/bson-bug/node_modules/rollup/dist/shared/rollup.js:2880:23)
          at ExpressionStatement$1.bind (/Users/kraenhansen/Projects/bson-bug/node_modules/rollup/dist/shared/rollup.js:2880:23)
          at Program$1.bind (/Users/kraenhansen/Projects/bson-bug/node_modules/rollup/dist/shared/rollup.js:2876:31)
          at Module.bindReferences (/Users/kraenhansen/Projects/bson-bug/node_modules/rollup/dist/shared/rollup.js:9599:18)

       

        1. rollup.config.js
          0.5 kB
        2. package.json
          0.4 kB
        3. main.js
          0.1 kB

            Assignee:
            neal.beeken@mongodb.com Neal Beeken
            Reporter:
            kraen.hansen@mongodb.com Kræn Hansen
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: