The error message after i run 'npm run backend'.
Code in .env file -
PORT=5000
MONGO_URI='mongodb://127.0.0.1:27017/test'
**code in index.js file - **
// packages
import path from "path";
import express from "express";
import dotenv from "dotenv";
import cookieParser from "cookie-parser";
// Utiles
import connectDB from "./config/db.js";
dotenv.config();
const port = process.env.PORT || 5000;
connectDB();
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.get("/api/config/paypal", (req, res) => {
res.send({ clientId: process.env.PAYPAL_CLIENT_ID });
});
app.listen(port, () => console.log(`Server running on port: ${port}`));
**code in db.js file - **
import mongoose from "mongoose";
const connectDB = async () => {
try {
console.log(process.env.MONGO_URI); // Add this line
await mongoose.connect(process.env.MONGO_URI);
console.log(`Successfully connected to MongoDB `);
} catch (error) {
console.error(`ERROR: ${error.message}`);
process.exit(1);
}
};
export default connectDB;
.env and db.js is in the folder config, index.js is in the folder utils
Learning react and javascript currently. I learn by watching tutorials online then creating my own projects.
Im trying to output 'Successfully connected to MongoDB ' in the db file but i get the URI error.
i tried printing process.env.MONGO_URI via console log and it prints undefined. I really need help and explanation why its not working
To make life easier for yourself, follow the deafult documentation and move your
.envfile into your root directory then update as follows: