Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB throwing error Module not found: 'module'

I have a Mongo db setup on localhost:27017 and and trying to connect to it from my app that uses Webpack via Mongoose. I have Mongoose installed 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'm pretty sure i've followed the documentation correctly but it's throwing the following compile 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

There is also another error in the console:

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

Anyone seen this before and know how to resolve it? Is there additional packages I might need to install in node?

like image 948
red house 87 Avatar asked Oct 20 '16 16:10

red house 87


People also ask

What is Tcmalloc in MongoDB?

MongoDB uses tcmalloc memory allocator to allocate and release memory back to system. Free memory can be released immediately after it is marked as free by setting the parameter tcmallocAggressiveMemoryDecommit to 1. It can be done both at startup time and at run time.

Why is MongoDB using so much memory?

The short answer is MongoDB relies on both its internal memory caches as well as the operating system's cache. The OS cache generally is seen as “Unallocated” by sysadmins, dba's, and devs. This means they steal memory from the OS and allocate it internally to MongoDB.

How can you check the amount of mapped memory that MongoDB is using?

You can inspect mem. mapped to check the amount of mapped memory that mongod is using. If this value is greater than the amount of system memory, some operations will require a page faults to read data from disk.

Is MongoDB memory intensive?

MongoDB is not an in-memory database. Although it can be configured to run that way. But it makes liberal use of cache, meaning data records kept memory for fast retrieval, as opposed to on disk. There is much bad information on StackOverflow about what to do when your server runs out of memory.


1 Answers

This error is because you're trying to use mongodb from browser, as create-react-app is a front-end app. You should use a back-end server and use mongodb from there.

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

like image 163
a_rahmanshah Avatar answered Oct 03 '22 17:10

a_rahmanshah