Module not found: Error: Can't resolve 'dns' when using MongoDB

I'm new to Reactjs, Nodejs and MongoDB. I'm currently trying to change Mediums snowflake tool to store users scores in a database. I have installed yarn, mongodb and mongodb-core through npm. It is a single page web application which is what I think is causing me trouble. I add

var MongoClient = require('mongodb'); 

To SnowflakeApp.js and encounter the following error:

Module not found: Error: Can't resolve 'dns' in '/home/mlAHO174/snowflake/node_modules/mongodb-core/lib'

I've tried googling this error and have discovered it could be a range of things. I'm not sure if it is because React is front end and I'm trying to alter back end or because mongoDB is installed incorrectly. I'm new to this so would be grateful for help!

1

7 Answers

DNS is a core module of Node.JS. Telling people they need to install DNS via NPM will end up with them having a completely different module that does something else.

vs

This error most likely means you are trying to do something from the client-side that needs to be done on the server-side. If MongoDB module can't find the DNS component, it's running on the client-side.

MongoDB has to run on the server. In order to access data from React dynamically you'll need to set up an API using something like Express or Apollo.

Update: A great way to do this is with Azure Functions (TypeScript) or AWS (Lambda) functions

1

For anyone who encounters this Error while importing the clientPromise (like in the with-mongodb template):

Make sure you're in the /pages/ directory!

It won't work in other directories like /components.

(and you should take a break or get some coffee...)

2

The problem is that you are trying to connect to the database from the front end. If this were possible that would open up a whole world of security issues. You need to set up your database connections on the backend and then have the front end make requests to the backend to handle the database.

1

I solved this by installing and using 'bson' instead of 'mongodb' for the client part of the code. 'bson' has a tiny bit of what 'mongodb' has and it might have what you are looking for. 'bson' is built for the browser.

In my case I needed the "ObjectId" in the browser and pulling it in from 'bson' did the trick as I didn't want to reference 'mongodb' because of the error described in the OP.

The other answers are also correct depending on why you're getting this error.

I think - mongo package is meant to be run on servers only, not in the browser.

It does not work inside Next.js page components, but does work inside getStaticProps, getServerSideProps, getStaticPaths etc - because they run on the server, not the client.

Alternative - Firebase Realtime database, you can access it in client-side code too via the REST API. Example - a website (say a React app) that is hosted on GitHub pages or some other static server, but doesn't have a web app server (aka backend).

For anyone that end up here from Google, my solution to this problem was to update the MongoDB package. By default, the NodeJS driver for MongoDB is at version 4.8, but the updated version as of writing those lines is 5.5 and is the recommended version in the official docs.

welcome to stack overflow.

You need to understand and learn few basics of web-applications. There's frontend, backend and a layer between them and a layer between backend and database. Frontend includes react.js, angular.js or anything else that is on browser. Backend is used to take request from frontend, providing API's to frontend and ask for data from other API's or database. Database includes sql, no-sql.

The error you are facing if of a NPM module mongodb-core.js. Either it's not installed properly, or installed using wrong version of module which is not comparable with your node version, or wrong version of NPM, or module using another NPM module which is not installed.

The issue in your case is mongodb-core uses a module dns which is not been installed. Try to install dns with npm i dns. or remove and install mongodb-core again.

4

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like