node-convict
node-convict copied to clipboard
Always uses default value
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!
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.
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 Facing the same issue. Did you solve it?
no, I removed convict from my setup and use process.env.MY_CONF_VAR || 'default value'
instead
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()
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.
import { config as loadEnv } from 'dotenv'
loadEnv({ path: 'PATH_TO_ENV_FILE' })