I am trying to learn full-stack web dev, and I am trying to set up MongoDB database to local ENV variables, but I am having problems. I do not know how to connect the `.env` file to my `server.js` file.

My `.env` file looks like this:

```
DATABASE_URL=mongodb://localhost/mybary

```

and `server.js` looks like this:

```
if (process.env.NODE_ENV !== 'production') {
    require('dotenv').parse()
  }

  const express = require('express')
  const app = express()
  const expressLayouts = require('express-ejs-layouts')

  const indexRouter = require('./routes/index')

  app.set('view engine', 'ejs')
  app.set('views', __dirname + '/views')
  app.set('layout', 'layouts/layout')
  app.use(expressLayouts)
  app.use(express.static('public'))

  const mongoose = require('mongoose')
  mongoose.connect(process.env.DATABASE_URL, { useNewUrlParser: true })
  const db = mongoose.connection
  db.on('error', error => console.error(error))
  db.once('open', () => console.log('Connected to Mongoose'))

  app.use('/', indexRouter)

  app.listen(process.env.PORT || 3000)

```

It throws this error:

```
src.toString().split(NEWLINES_MATCH).forEach(function (line, idx) {
      ^

TypeError: Cannot read properties of undefined (reading 'toString')
    at Object.parse (C:\Users\tangc\Desktop\site\node_modules\dotenv\lib\main.js:43:7)
    at Object.<anonymous> (C:\Users\tangc\Desktop\site\server.js:4:23)

```

I believe the problem is in line two at `require('dotenv').parse()`, as I don't believe I am using that right, because when the server code looks like this it works (hard-coded the url):

```
const express = require('express')
  const app = express()
  const expressLayouts = require('express-ejs-layouts')

  const indexRouter = require('./routes/index')

  app.set('view engine', 'ejs')
  app.set('views', __dirname + '/views')
  app.set('layout', 'layouts/layout')
  app.use(expressLayouts)
  app.use(express.static('public'))

  const mongoose = require('mongoose')
  mongoose.connect('mongodb://localhost/mybary', { useNewUrlParser: true })
  const db = mongoose.connection
  db.on('error', error => console.error(error))
  db.once('open', () => console.log('Connected to Mongoose'))

  app.use('/', indexRouter)

  app.listen(process.env.PORT || 3000)

```

I can't find a way to fix this on the web. If anything isn't clear, please comment.