MongoDB throw error Module not found: 'module'

I have a Mongo db setup on localhost: 27017 and am trying to connect to it from my application that uses Webpack through Mongoose. I have installed Mongoose as a package. Here is my code:

import mongoose from 'mongoose'; var db = mongoose.connect('mongodb://localhost:27017/music-app'); mongoose.connection.once('connected', function() { console.log("Connected to database") }); 

I am sure I was following the documentation correctly, but this threw the following compilation error:

 Error in ./~/mongoose/~/mongodb/~/mongodb-core/~/require_optional/~/resolve-from/index.js Module not found: 'module' in C:\Users\new\Desktop\Development Projects\music-app\node_modules\mongoose\node_modules\mongodb\node_modules\mongodb-core\node_modules\require_optional\node_modules\resolve-from 

The console also has another error:

 webpackHotDevClient.js:216 Error in ./~/mongoose/~/mongodb/lib/mongo_client.js Module not found: 'dns' in C:\Users\new\Desktop\Development Projects\music-app\node_modules\mongoose\node_modules\mongodb\lib @ ./~/mongoose/~/mongodb/lib/mongo_client.js 12:10-24 

Has anyone seen this before and knows how to solve it? Are there any additional packages that I might need to install in node?

+5
source share
1 answer

This error occurs because you are trying to use mongodb in a browser, since create-react-app is an external application. You must use the server server and use mongodb from there.

You can check out this full repo with a full stack having a nodejs server with create-react-app interface. https://github.com/fullstackreact/food-lookup-demo

+3
source

Source: https://habr.com/ru/post/1258558/


All Articles