Webpack will not compile when I try to import a mongoose model Webpack will not compile when I try to import a mongoose model mongoose mongoose

Webpack will not compile when I try to import a mongoose model


The problem you have is that you're mixing client (React) and server side code (Mongoose). While both of them are JS, Mongoose relies on some built-in Node.js modules and Webpack can't resolve those modules, unless your target is node which it will ignore them (You can learn more about it here).

I see what you were trying to do, call Mongoose models from your React components and fetch data from there, right?... that's not the way it works.

The solution here would be removing those Mongoose models, separate your code (client/server), make an API and handle all your needs from there (Create, Read, Update, Delete all your resources), here's a video tutorial that will help you to create an API really quickly using node-restful but just for demo/practice purposes and this other one for fetching data (an easy and simple way).


For client-side application you should use 'mongoose/browser'. It works with Webpack.