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));
this package.json file of the server folder:
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"start": "nodemon index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.0",
"cors": "^2.8.5",
"dotenv": "^16.0.0",
"dotenv-config": "^0.1.1",
"express": "^4.17.3",
"mongoose": "^6.2.10",
"nodemon": "^2.0.15"
}
}
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.
2
Answers
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:
Also some tips:
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!