Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
process.env variable returns undefined from a specific file

I have got a configuration file called dev.js which contains an object that has a key called dbURL which holds database url for MongoDB. It looks like this exactly.

export const config = {
    dbURL: 'mongodb+srv://...'
}

And I have got another file called db.js where I import the above dev.js file and use the dbURL key in order to connect to the database.

import { connect as _connect } from 'mongoose';
import {config} from '../config/dev';

const connect = (url = config.dbURL, opts = {}) => {
    return _connect(
        url,
        {}, err => {
            if (err) throw err;
            console.log('Connected to MongoDB!')
        }
    )
}
export default connect;

The problem is if I tried to change the dbURL's value in the dev.js file to be process.env.DEV_DB for example.

export const config = {
    dbURL: process.env.DEV_DB
}

The .env file looks like this

DEV_DB = "mongodb+srv://...."

It actually returns undefined, although I tried to console.log(process.env.DEV_DB) in the db.js file and it printed the database url normally.

import { connect as _connect } from 'mongoose';
import {config} from '../config/dev';

const connect = (url = config.dbURL, opts = {}) => {
    console.log(config) //logs {dbURL: undefined}
    console.log(process.env.DEV_DB) //logs the database url normally
    return _connect(
        url,
        {}, err => {
            if (err) throw err;
            console.log('Connected to MongoDB!')
        }
    )
}
export default connect;

When I tried to log the process.env.DEV_DB in the dev.js file, it logged undefined as well.

Note: The dotenv is imported at the top of the application, and the .env is in the root file.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Have you imported dotenv at the top of your dev.js file as well? That solved it for me when I tried it locally.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!