Reputation: 983
I am in the process of deploying my MERN app to a Digital Ocean droplet (Ubuntu 20.04 server).
I have cloned my GitHub repo to the droplet, installed the dependencies using npm install
. Next, when I am starting the server using npm start
, I get the following error:
The error essentially says that the first parameter to mongoose.connect()
is undefined and must be a string. However, everything works fine in my local machine and when I console.log process.env.MONGO_URI
, I get the connection string.
server/config/db.js
const mongoose = require("mongoose");
const colors = require("colors");
const dotenv = require("dotenv");
dotenv.config();
const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
});
console.log(`MongoDB connected: ${conn.connection.host}`.cyan.bold);
} catch (error) {
console.error(`Error: ${error.message}`.red.bold.underline);
process.exit(1);
}
};
2;
module.exports = connectDB;
Why am I getting this error while starting the server in my Digital Ocean droplet?
Upvotes: 0
Views: 968
Reputation: 57095
dotenv
doesn't load system variables.
Create .env
file with
MONGO_URI=XXXXXXX
https://github.com/motdotla/dotenv#usage
Create a .env file in the root directory of your project. Add environment-specific variables on new lines in the form of NAME=VALUE. For example:
DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3
Upvotes: 1