Mongoose: Error importing mongoose on clientside using es6 import, webpack, and create-react-app

Created on 1 Sep 2017  路  12Comments  路  Source: Automattic/mongoose


4.11.9

Do you want to request a feature or report a bug?
Report a Bug

What is the current behavior?

Error when loading page that imports mongoose on the client side when using webpack through create-react-app.

This line of code:

new webpack.DefinePlugin({ 'typeof window': '"object"' })

from
http://mongoosejs.com/docs/browser.html

does not appear to fix it.

The error:
脳 TypeError: Object prototype may only be an Object or null: undefined
(anonymous function)
node_modules/mongoose/lib/browserDocument.js:99

If the current behavior is a bug, please provide the steps to reproduce.
npm install -g create-react-app
create-react-app foo
cd foo
npm install --save mongoose

edit App.js to add to the first line:
import mongoose from "mongoose";

npm start // or yarn start if using yarn

What is the expected behavior?

App loads without error.

Please mention your node.js, mongoose and MongoDB version.
node.js 7.2.0
mongoose 4.11.9
MongoDB ... irrelevant, this is client side

screen shot 2017-09-01 at 2 39 52 pm

won't fix

Most helpful comment

Please let us know for any updates...

All 12 comments

Also filed with create-react-app

https://github.com/facebookincubator/create-react-app/issues/3050#event-1232121738

comment by gaearon on that site:
"Please file an issue with mongoose. The requirement to add a plugin to webpack configuration to consume a library is not great and puts a complexity tax on the ecosystem. I believe it is completely avoidable on the mongoose side if they prioritize fixing it.

As a temporary fix, you can always use mongoose as a

@varunjayaraman yeah that's expected behavior AFAIK. We include bson to get ObjectIds without having to go through the driver, because importing the driver itself made browserify blow up.

what's confusing to me is that it only fails with create-react-app

Yeah I honestly have no idea and don't want to spend more time trying to support create-react-app. Its a very niche edge case for mongoose.

Same error here when including a second reference to a Mongoose schema. Any news on a fix?

browserDocument.js:99 Uncaught TypeError: Object prototype may only be an Object or null: undefined
at Function.create ()
at Object. (browserDocument.js:99)
at Object. (browserDocument.js:294)
at __webpack_require__ (bootstrap 016093d5014cbaec899c:555)
at fn (bootstrap 016093d5014cbaec899c:86)
at Object. (document_provider.web.js:8)
at __webpack_require__ (bootstrap 016093d5014cbaec899c:555)
at fn (bootstrap 016093d5014cbaec899c:86)
at Object. (embedded.js:7)
at __webpack_require__ (bootstrap 016093d5014cbaec899c:555)

Nope no progress.

Please let us know for any updates...

Happens for me without using create-react-app

Was this page helpful?
0 / 5 - 0 ratings

Related issues

simonxca picture simonxca  路  3Comments

gustavomanolo picture gustavomanolo  路  3Comments

p3x-robot picture p3x-robot  路  3Comments

Igorpollo picture Igorpollo  路  3Comments

ArThoX picture ArThoX  路  3Comments