Mongo Db does not connect in node js

Tell us what’s happening:

I Cannot connect to the database and it keeps showing me this error
" .env: line 2: DATABASE: command not found"

Your code so far

  'use strict';

const express     = require('express');
const bodyParser  = require('body-parser');
const fccTesting  = require('./freeCodeCamp/fcctesting.js');
const passport = require('passport');
const session = require('express-session');
const ObjectID = require('mongodb').ObjectID;
const mongo = require('mongodb').MongoClient;

const app = express();

fccTesting(app); //For FCC testing purposes
app.use('/public', express.static(process.cwd() + '/public'));
app.use(bodyParser.urlencoded({ extended: true }));
 app.set('view engine', 'pug');
  secret: process.env.SESSION_SECRET,
  resave: true,
  saveUninitialized: true,

passport.serializeUser((user, done) => {
  done(null, user._id);

mongo.connect(process.env.DATABASE, (err, db) => {
  if(err) {
    console.log('Database error: ' + err);
  } else {
    console.log('Successful database connection');

    //serialization and app.listen
    passport.deserializeUser((id, done) => {
    {_id: new ObjectID(id)},
      (err, doc) => {
        done(null, doc);

  .get((req, res) => {
    res.render(process.cwd() + '/views/pug/index.pug', {title: 'Hello', message: 'Please login'});

app.listen(process.env.PORT || 3000, () => {
  console.log("Listening on port " + process.env.PORT);
````Preformatted text`

File .env

**Your browser information:**

User Agent is: <code>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36</code>.

**Challenge:** undefined

**Link to the challenge:**

Hard to tell without looking into .env, but it seems like something in line 2 makes DATABASE a command. Did you try to take url in quotes?

here is my .env
DATABASE = “mongodb://”;

i changed the user and password here only but they are real in the original file

Where about in the challenges are you?

SESSION_SECRET='anything' // add quotes here
DATABASE = 'mongodb://' //remove “;”

1 Like

this challenge and it keeps failing because it’s unable to connect to the database
i used console.log(process.env.DATABASE) and it returned undefined

make sure you have quotes for this

DATABASE = ‘mongodb://’

i edited it this way

my .env for this section is:


^replaced all the good stuff with ‘sdfkjlhsd’

1 Like

what if you console.log process.env.SESSION_SECRET ?

Can you post your Glitch project here? I can remix/add my .env info and see if it posts.

Your code connects for me when I put in my .env info:

1 Like

the problem is in my database then, i created a new one on mLab and it kept showing me the same error, is there another website i can use to create a database?

try: mongodb+srv://' in quotes for the URI

edit: obviously add your Username and PW into that


5:07 PM

throw new ERR_INVALID_ARG_TYPE(‘url’, ‘string’, url);
TypeError [ERR_INVALID_ARG_TYPE]: The “url” argument must be of type string. Received type undefined

5:07 PM

at Url.parse (url.js:154:11)

5:07 PM

at Object.urlParse [as parse] (url.js:148:13)

5:07 PM

at module.exports (/rbd/pnpm-volume/be841e9b-199f-4bd5-a3d9-4524af514f94/node_modules/

5:07 PM

at connect (/rbd/pnpm-volume/be841e9b-199f-4bd5-a3d9-4524af514f94/node_modules/

5:07 PM

at Function.MongoClient.connect (/rbd/pnpm-volume/be841e9b-199f-4bd5-a3d9-4524af514f94/node_modules/

make sure you put it in quotes

DATABASE = ‘mongodb+srv://’

DATABASE = ‘mongodb**+srv**://’

*edit 4: i can’t bold the +srv part. no *'s

is it possible that the problem is in my pc?

is it possible that the problem is in my pc and not my code ?

Your code worked fine for me WIN10 /Chrome with my .env info. You could try a different browser?