title: Node.js can't recognise any variables in .env
body: <p>I am following the fullstackopen.com course and I seem to have an issue with my .env file, currently when I try to connect to the database I get this error:</p>
  <pre><code>error connecting to MongoDB The `uri` parameter to `openUri()` 
  must be a string, got &quot;undefined&quot;. Make sure the first parameter to 
  `mongoose.connect()` or `mongoose.createConnection()` is a string.
  </code></pre>
  <p>I have figured out the process.env variable is not being read properly by Node.js from checking previous answers most of the issue has been around not having dotenv imported correctly my code has this so I don't think this could be the issue. I have also printed the .env variable to console and it is undefined. My .env file is also in the root of the project so I don't think it is that either.</p>
  <p>I have included my .env file and the files being used to call the code below.</p>
  <p>.env file</p>
  <pre><code>MONGODB_URI='mongodb+srv://fullstackopen:&lt;MyPasswordisHERE&gt;@cluster0.brwcy.mongodb.net/myFirstDatabase?retryWrites=true&amp;w=majority'
  PORT=3001
  </code></pre>
  <p>note.js application</p>
  <pre><code>require('dotenv').config()
  const mongoose = require('mongoose')
  const url = process.env.MONGODB_URI
  console.log('connecting to', url)
  
  mongoose.connect(url)
      .then(result =&gt; {
          console.log('connected to MongoDB')
      })
      .catch((error) =&gt; {
          console.log('error connecting to MongoDB', error.message)
      })
  
  const noteSchema = new mongoose.Schema({
      content: String,
      date: Date,
      important: Boolean,
  })
  
  noteSchema.set('toJSON', {
      transform: (document, returnedObject) =&gt; {
          returnedObject.id = returnedObject._id.toString()
          delete returnedObject._id
          delete returnedObject.__v
      }
    })
    
  module.exports = mongoose.model('Note', noteSchema)
  </code></pre>
  <p>index.js</p>
  <pre><code>require('dotenv').config()
  const { request, application, response } = require('express')
  const express = require('express')
  const app = express()
  const Note = require('./models/note')
  app.use(express.json())
  app.use(express.static('build'))
  
  
  const cors = require('cors')
  app.use(cors())
  
  
    app.get('/', (request, response) =&gt; {
      response.send('&lt;h1&gt;Hello World&lt;/h1&gt;')
    })
  
    app.get('/api/notes/:id', (request, response) =&gt; {
        const id = Number(request.params.id)
        const note = notes.find(note =&gt; note.id === id)
        if(note){
          response.json(note)
        }
        else {
          response.status(404).end()
        }
      })
  
    app.get('/api/notes',(request, response) =&gt; {
        Note.find({}).then(notes =&gt; {
          console.log(response)
          response.json(notes)
        })
    })
  
    app.delete('/api/notes/:id', (request, response) =&gt; {
      const id = Number(request.params.id)
      notes = notes.filter( note =&gt; note.id !== id)
  
      response.status(204).end()
    })
  
    const generateId = () =&gt; {
      const maxId = notes.length &gt; 0 
      ? Math.max(...notes.map(n =&gt; n.id))
      : 0
  
      return maxId + 1
    }
  
    app.post('/api/notes', (request, response) =&gt; {
     
      const body = request.body
  
      if(!body.content){
        return response.status(400).json({
          error: 'content missing'
        })
      }
  
      const note = {
        content: body.content,
        important: body.important || false,
        date: new Date(),
        id: generateId(),
      }
      
      notes = notes.concat(note)
      response.json(note)
  
  
    })
  
    const unknownEndpoint = (request, response) =&gt; {
      response.status(404).send({error: 'unknown endpoint'})
    }
  
    app.use(unknownEndpoint)
  
    const PORT = process.env.PORT
    app.listen(PORT, ()=&gt; {
        console.log(`Sever is running on port ${PORT}`)
    })
  
  
  </code></pre>
  <p>I know that I have dotenv being imported in note.js and index.js, the reason for this is when I was testing why the .env was't being recognised I checked the note.js file by running that only using the command below, however in production the import is only in index.js so that isn't the issue</p>
  <pre><code>node note.js
  </code></pre>
  <p>My file structure for the project is also included below</p>
  <pre><code>.  ..  build  .env  .git  .gitignore  index.js  models  mongo.js  node_modules  package.json  package-lock.json  Procfile  requests
  
  </code></pre>