Search code examples
deploymentweb-deploymentdigital-ocean

Unable to start the server in a digital ocean droplet


I am trying to deploy my MERN app to a digital ocean droplet (Ubuntu 20.04 server).

I cloned my GitHub repo to the server. enter image description here

Now, when I am trying to start the server using npm start, I get the following error. enter image description here The code snippet is as follows:

server/config/db.js

const mongoose = require("mongoose");
const colors = require("colors");

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;

However, everything works fine on my local machine. If I console.log(process.env.MONGO_URI), I get the string.

In the droplet, I tried doing the following:

export MONGO_URI=the_connection_string. Even then, I am getting the error.

What am I doing wrong?

ERROR enter image description here


Solution

  • I found the issue. I had put .env inside .gitignore. Therefore .env was not available in the github repo, which I had cloned to my Digital Ocean droplet. As a solution, I recreated the .env file inside my droplet using vim. Then I could start the server without any issue.