title: MERN Stack - Can't connect to MongoDB with URI string
body: <p>I cannot connect my MERN application to mongoDB with a URI string in a .env file. The error I am getting is</p>
  <pre><code>URI must include hostname, domain name, and tld
  [nodemon] app crashed - waiting for file changes before starting... 
  </code></pre>
  <p>I am using a file called db.js (inside of a config folder):</p>
  <pre><code>
  const connectDB = async () =&gt; {
    try {
      await mongoose.connect(process.env.MONGOURI, {
      });
  
      console.log(`MongoDB Connected...`);
    } catch (err) {
      console.error(err.message);
      // Exit process with failure
      process.exit(1);
    }
  };
  
  module.exports = connectDB;
  </code></pre>
  <p>Here is my .env file which is in the root folder:</p>
  <pre><code>PORT = 5000
  MONGOURI =
      &quot;mongodb+srv://&lt;my_username_is_here&gt;:&lt;password_is_here&gt;@gmttcluster.cvloz.mongodb.net/myFirstDatabase?retryWrites=true&amp;w=majority&quot; 
  </code></pre>
  <p>I call this all from a script called server.js in the root directory</p>
  <pre><code>const express = require(&quot;express&quot;);
  const colors = require(&quot;colors&quot;);
  const dotenv = require(&quot;dotenv&quot;).config();
  const { errorHandler } = require(&quot;./middleware/errorMiddleWare&quot;);
  const connectDB = require(&quot;./config/db&quot;);
  const port = process.env.PORT || 5000;
  
  connectDB();
  
  const app = express();
  
  app.use(express.json());
  app.use(express.urlencoded({ extended: false }));
  
  app.use(&quot;/home&quot;, require(&quot;./routes/teeTimeRoutes&quot;));
  
  app.use(errorHandler);
  
  app.listen(port, () =&gt; console.log(`Server started on port ${port}`));
  </code></pre>
  <p>Here is another error I get when I take the &quot;&quot; out of the .env file</p>
  <pre><code>Invalid scheme, expected connection string to start with &quot;mongodb://&quot; or &quot;mongodb+srv://&quot;
  </code></pre>