简体   繁体   中英

Setting up the environment vars in node before the config module reads the config from default.json

Comparatively new to JS and node,

Requirement: Need to set the environment variables after reading it from a secure store before the config module is loaded.

Details: We have an ask where we need to read the secrets from a secret store and make it available to the node environment before the config module kicks in and assigns values using the default.js.

I tried splitting the express app setup and initialization and tried reading the secrets in between those steps, but to no avail.

app.js -> Setup the app

index.js -> 
// Setup the environments with the secrets
import config from 'config'
import {app_init} from './app' // Setting up the express app

// Now setting up the env variables
process.env.DB_HOST = 'MyDbHost'
process.env.DB_PASS = 'DBPass' // This will be read from the secret store

log.info(`DB_HOST used in ENV: ${process.env.DB_HOST}`) // Prints the right value
log.info(`DB_HOST used in config: ${config.database.host}`) // Prints undefined

Anyway, Is there a way to inject the secrets in the env before the config module reads them?

Please let me know if you need more info.

Looking at the config documentation, especially here one can use either deferred configuration and use full nodejs capabilities to create the configuration.


In my case, i am planning to read the secrets from the store and published them as the environment variables. I will publish if I see challenges doing that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM