Home > OS >  MongoDB Environment Variables Error, Unsure How to Proceed
MongoDB Environment Variables Error, Unsure How to Proceed

Time:11-12

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.

CodePudding user response:

If you use node to run server.js, you can run it like this

node -r dotenv/config server.js 

or

export DATABASE_URL = mongodb://localhost/mybary
node server.js

FYI: Export the values on the server, it should be easy to manage different environments. For example, different servers have different ip and database hosts. When I install it, I don’t need to set any configuration, just overwrite.

CodePudding user response:

First - make sure that your .env file is in the root directory of your project.

Second - require dotenv as earliest as possible in your code require("dotenv").config();

Third - you shouldn't have any white spaces in your .env file. So DATABASE_URL = mongodb://localhost/mybary should be DATABASE_URL=mongodb://localhost/mybary

  • Related