node-convict icon indicating copy to clipboard operation
node-convict copied to clipboard

Always uses default value

Open bayerlse opened this issue 4 years ago • 7 comments

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!

bayerlse avatar Aug 28 '20 06:08 bayerlse

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.

brianjenkins94 avatar Sep 25 '20 02:09 brianjenkins94

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

Silur avatar May 12 '21 16:05 Silur

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

AlexZeitler avatar Jul 10 '21 00:07 AlexZeitler

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

Silur avatar Jul 10 '21 12:07 Silur

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()

AlexZeitler avatar Jul 10 '21 23:07 AlexZeitler

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

omarabdelaz1z avatar Feb 15 '22 01:02 omarabdelaz1z

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

ybelakov avatar Apr 07 '22 09:04 ybelakov