mongoose: Browser bundle contains NodeJS code that throws "Uncaught TypeError: Cannot read properties of undefined (reading 'split')"

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.3.8

Node.js version

16.14.0

MongoDB server version

4.2.19

Description

When bundling up mongoose for the browser this code is included from the file dist/browser.umd.js:

e.nodeMajorVersion=parseInt(t.versions.node.split(".")[0],10);

It throws this error in Chrome:

Uncaught TypeError: Cannot read properties of undefined (reading 'split')

My guess is that the code line above was never supposed to end up in the browser bundle because it is server-side node code.

Steps to Reproduce

See the browser.umd.js file in the dist folder of mongoose.

Expected Behavior

The browser.umd.js contains only browser-compatible code.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (3 by maintainers)

Commits related to this issue

Most upvoted comments

I am also experiencing this issue with mongoose 6.5.5 on node 16.17.

I can reproduce with a simplified example:

❯ cat package.json
{
  "name": "mongoose-error",
  "version": "1.0.0",
  "dependencies": {
    "mongoose": "~6.5.5"
  }
}
❯ npm i

added 28 packages, and audited 29 packages in 820ms

4 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
❯ grep -r "nodeMajor" node_modules/mongoose/*
node_modules/mongoose/dist/browser.umd.js:e.noop=function(){},e.errorToPOJO=function(t){if(!(t instanceof Error))throw new Error("`error` must be `instanceof Error`.");var e,r={},o=n(Object.getOwnPropertyNames(t));try{for(o.s();!(e=o.n()).done;){var i=e.value;r[i]=t[i]}}catch(t){o.e(t)}finally{o.f()}return r},e.nodeMajorVersion=parseInt(t.versions.node.split(".")[0],10)}).call(this,r(10))},function(t,e,r){"use strict";(function(t){

Downgrading to mongoose 6.5.2 removes the nodeMajorVersion reference.