Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
I'm trying to connect my MERN stack application to MongoDB by using mongoose but I'm getting this error even if I am using dotenv file or directly.
I've search for many solutions but didn't work for me, please help if you know what's the problem.
Is it because the dotenv file is not present in the root folder? I don't know what should be the root folder, please tell me if you know.
My index.js file in server folder:
import express from 'express';
import bodyparser from 'body-parser';
import mongoose from 'mongoose';
import cors from 'cors';
import dotenv from 'dotenv';
import postRoutes from './routes/posts.js'
const app = express();
dotenv.config();
app.use('./posts',postRoutes);
app.use(bodyparser.json({limit: '30mb',extended: true}));
app.use(bodyparser.urlencoded({limit: '30mb',extended: true}));
app.use(cors());
const PORT = process.env.PORT || 5000;
mongoose.connect((process.env.MONGO_URI, {useNewUrlParser:true, useUnifiedTopology: true}))
.then(()=> app.listen(PORT, ()=> console.log( `server running: ${PORT}`)))
.catch((error) => console.log(error.message));
and the error I'm getting while starting server:
[nodemon] clean exit - waiting for changes before restart
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
The `uri` parameter to `openUri()` must be a string, got "object".
Make sure the first parameter to `mongoose.connect()` or `
mongoose.createConnection()` is a string.
I've seen few solution already on net but didn't work. The server is still not running and I can't work on other modules , please help.
When I don't use the env file and directly put the mongo string , still getting same error and also I didn't replace mydatabasename in mongo url .
and the error I'm getting while starting server:
[nodemon] clean exit - waiting for changes before restart
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
The `uri` parameter to `openUri()` must be a string, got "object".
Make sure the first parameter to `mongoose.connect()` or `
mongoose.createConnection()` is a string.
What should I write now that's all my problem. I think I have given all important details. I think I've no other options instead of copy and pasting same thing again.
I understand the frustration I have been there before. Don't give up!
It looks like you got it right for the most part but I am having trouble reading below this line const PORT = process.env.PORT || 5000;
I would handle it like this:
const PORT = process.env.PORT || 5000;
// your code above ^^
const connectionString = ``;
mongoose.connect(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => {
// we're connected!
* Server Activation
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
Also some tips:
keep your .env files at the same level as the package.json file, that is typically the "root" of the project.
While debugging the issue its best to ignore environment variables put the content directly so that you can rule out external factors
–
–
Thank you @Trevor Njeru!! 😁
I had the same error and I solved. It was due the location of the .env file. I moved it so it rest along the root directory which is the one that have the package.json files. (also i did not applied the solution provided by other people: I did not specified the "path": require('dotenv').config(path: "./.env"). No. I just leaved as require('dotenv').config().
By just assuring the .env lives along the Server's side package.json, it worked for me.
Thank you once again!
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.