Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always uses default value #378

Open
bayerlse opened this issue Aug 28, 2020 · 7 comments
Open

Always uses default value #378

bayerlse opened this issue Aug 28, 2020 · 7 comments

Comments

@bayerlse
Copy link

Hello There,

I have following example:

export const config = convict<ConfigSchema>({
    env: {
        doc: 'The application environment.',
        default: ENV.LIVE, // === "production"
        format: ['production', 'development', 'test'],
        env: 'NODE_ENV'
    },
    dbKeyFile: {
        doc: 'Database key file path',
        format: String,
        default: '.tmp/keyfile.json',
        env: 'DB_KEY_FILE'
    }
});

When I run my code with webpack mode --development (which sets NODE_ENV) and do:

console.log(config.get('env'))
it returns always the default value: "production"

console.log(process.env.NODE_ENV)
it return the correct value: "development"

What is wrong here? Shouldnt get convict the value from process.env?

Thanks!

@brianjenkins94
Copy link

brianjenkins94 commented Sep 25, 2020

The problem is actually that webpack is setting process.env.NODE_ENV=development and not setting the actual NODE_ENV environment variable to development.

Here was my workaround:

https://github.com/brianjenkins94/kerplow/blob/master/dotfiles/express/ts/config/index.ts#L34-L41

Sorry, I misremembered. That code solves a similar but different problem.

@Silur
Copy link

Silur commented May 12, 2021

Experiencing the same issue on 6.1.0, but without webpack. No matter what I set the corresponding env var, it always uses the default value:

// config.js
const config = convict({
  env: {
    doc: 'The application environment.',
    format: ['production', 'development', 'test'],
    default: 'development',
    env: 'NODE_ENV'
  },
  host: {
    doc: 'Listen host for server',
    format: '*',
    default: '127.0.0.1',
    env: 'HOST'
  },
  port: {
    doc: 'Listen port for server',
    format: 'port',
    default: 3000,
    env: 'PORT'
  }
})
module.exports = config

// server.js
const config = require('./config.js')
const hostAtShell = process.env.HOST // this value is correct
const host = config.get('db.host') // Always returns the default 127.0.0.1
console.log(hostAtShell === host) // false

@AlexZeitler
Copy link

@Silur Facing the same issue. Did you solve it?

@Silur
Copy link

Silur commented Jul 10, 2021

no, I removed convict from my setup and use process.env.MY_CONF_VAR || 'default value' instead

@AlexZeitler
Copy link

AlexZeitler commented Jul 10, 2021

Looks like my issue has been different. It didn't notice process.env.MY_CONF_VAR='some-value' but it did work when I set the environment variables when starting the process or using this in my config.ts file and using a .env file:

import { config as loadEnv } from 'dotenv'
loadEnv()

@omarabdelaz1z
Copy link

no, I removed convict from my setup and use process.env.MY_CONF_VAR || 'default value' instead

Totally agree with you, I removed it now.
image

@ybelakov
Copy link

ybelakov commented Apr 7, 2022

import { config as loadEnv } from 'dotenv'
loadEnv({ path: 'PATH_TO_ENV_FILE' })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants