1

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

HKS
  • 526
  • 8
  • 32
  • Did you try run npm install after cloning repo? – ozlevka Mar 29 '21 at 06:05
  • Yes, I did npm install after cloning the repo. I am getting the error when I try to run the server using npm start. I am loading the MONGO_URI from the .env file as the first paramater to mongoose.connect( ). This all works fine in my local machine. I dont' know why I am getting the error while in the droplet. – HKS Mar 29 '21 at 07:12
  • Cool, Where mongodb is running? How you configure this variable process.env.MONGO_URI? – ozlevka Mar 29 '21 at 07:13
  • I am using MongoDB Atlas. What do you mean when you say how did I configure this variable? Can you please elaborate? – HKS Mar 29 '21 at 07:15
  • Sure. According to an image with an error message. mongodb URI is undefined. According to code, you sharing URI received via MONGO_URI env variable. So I guess that this variable is undefined. Try run `export MONGO_URI=[url of your atlas cluster]; npm start`. – ozlevka Mar 29 '21 at 07:19
  • I am still getting the same error. Should I write the URL inside square brackets? I just wrote `export MONGO_URI = url_of_atlas_cluster` – HKS Mar 29 '21 at 07:28
  • I have added the screenshot of the error in the question – HKS Mar 29 '21 at 07:36
  • MONGO_URI = url_of_atlas_cluster, replace spaces... This is shell command – ozlevka Mar 29 '21 at 07:36
  • If you the screenshot, there is no space. – HKS Mar 29 '21 at 07:38
  • Print variable before executes connect. – ozlevka Mar 29 '21 at 07:38
  • Add `console.log(process.env.MONGO_URI)` before `const connectDB = async () => {`. – ozlevka Mar 29 '21 at 07:46
  • Oops, It is undefined. What could be wrong? – HKS Mar 29 '21 at 07:49
  • I fixed the issue on my local machine. Console logging `process.env.MONGO_URI` shows the string. Then I pushed the repo to GitHub, cloned the repo to my droplet again (after deleting the previous directory). Then set MONGO_URI as export MONGO_URI=url_of_atlas_cluster. The I ran `npm start`. And I am still getting the same error. – HKS Mar 29 '21 at 08:16
  • Look this https://stackoverflow.com/questions/25112510/how-to-set-environment-variables-from-within-package-json – Hector Vido Mar 29 '21 at 19:33
  • @HKS is the issue resolved here? – Tushar Gupta - curioustushar Mar 31 '21 at 08:52
  • Yes. The issue is resolved. – HKS Mar 31 '21 at 08:53

1 Answers1

0

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.

HKS
  • 526
  • 8
  • 32