This is my .js file:
import express from "express";
import bodyParser from "body-parser";
import mongoose from "mongoose";
import cors from "cors";
import dotenv from "dotenv";
import multer from "multer";
import helmet from "helmet";
import morgan from "morgan";
// Below comes with node so we didn't install it.
import path from "path";
import { fileURLToPath } from "url";
// These Two are used to setup paths when we are using directories.
/*CONFIGURATIONS
These will include middleware config as well as package config*/
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
dotenv.config();
const app = express();
app.use(express.json())
app.use(helmet());
app.use(helmet.crossOriginResourcePolicy({policy:"cross-origin"}));
app.use(morgan("common"));
app.use(bodyParser.json({limit: "30mb", extended : true}));
app.use(bodyParser.urlencoded({limit:"30mb", extended:true}));
app.use(cors()) //Cross Orign Resource Policies.
app.use("/assets", express.static(path.join(__dirname, 'public/assets')));
//The above line does is it sets dir where we keep our assets. In our cse we do it locally. In actual case we do it in a proper storage system
/*FILE STORAGE */
const storage = multer.diskStorage({
destination : function(req, file, cb){
cb(null, "public/assests");
},
//When someone uploads a photo into the wpage, it's gonna save in the public/assets folder.
filename : function(req, file, cb ){
cb(null, file.originalname);
}
})
const upload = multer({storage});
//This will help us save the img. Anytime we are going to upload a file we use the variable upload.
//Go to mongoDB website and create a datbase cluster.
//.env file is created.
/*MONGOOSE SETUP */
const PORT = process.env.PORT || 6001;
mongoose.connect(process.env.MONGO_URL, {
useNewUrlParser : true,
useUnifiedTopology : true,
}).then(() => {
app.listen(PORT, () => console.log(`Server Port : ${PORT}`));
}).catch((error) => console.log( `${error} did not connect`))
This is my .env file:
MONGO_URL = 'mongodb+srv://username:[email protected]/?retryWrites=true&w=majority'
PORT = 3001
Error i am facing is :
MongooseError: The uri
parameter to openUri()
must be a string, got "undefined". Make sure the first parameter to mongoose.connect()
or mongoose.createConnection()
is a string. did not connect
Plz help me.
I want the output to print the port number.
2
Answers
Try
import dotenv/config
at the top, instead of the current import you have.This is the correct way per the documentation: https://www.npmjs.com/package/dotenv?activeTab=readme#how-do-i-use-dotenv-with-import
I faced a similar issue few months back and solved it by changing
.env
to.parsed
.I’ve just checked
dotenv
package source code and couldn’t see.env
functionality. So, it might be officially replaced by.parsed
.So, you might just need to do the following in your code:
For testing:
.env
file in project root, and addTOKEN=MYTOKEN
.dotenv
package (npm i dotenv).index.js
file.node index.js
MYTOKEN
should be logged in the console.