Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

118
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda