简体   繁体   中英

Dynamic settings in config.json from environment

In my config.json, I have a requirement for a setting object to be populated dynamically based on the environment variable setting.

"somesetting": {
    "setting1": "%S1%",
    "setting2": "%S2%"
}

My environment is Windows.

Many examples which I have come across have hard coded values and I am unable to understand how to handle my scenario. I am very new to npm and the javascript world. Any advice / references in this matter is really appreciated

Edit 2: Possibly,I missed it, but my situation is that values of the %S1% and %S2% could be different in different environments. Consider S1 and S2 as some paths which are likely to be different based on which machine my code builds on the cloud eg C:\\xyz or D:\\xyz etc, which I would have no way of knowing upfront. So this means I can't have a static setting in it even if my environments are different. Thanks

JSON is a static file and its content is a string, so you can't use variables here.

You can try to rewrite json file via javascript using fs module readFile and writeFile .

Maybe template engines like Handlebars and Mustache will help you to do that much easier.

But usually developers use one static file for one environment. You can change config only before starting your application, otherwise you can break it in a runtime (so bad for your users and customers).

Have a look at this https://www.npmjs.com/package/config It's JSON-based config. We use this package for many years and it's quite good for a few environments.

Infrastructure

config/
- default.json
- development.json
- staging.json
- production.json
app.js
*/

Usage

/*  default.json */
{
  "HTTP":  {
    "PORT": 3001,
    "MESSAGE": "Hello!"
  }
}
/*  development.json */
{
  "HTTP":  {
    "PORT": 4001
  }
}
/* app.js */
const config = require('config');

const message = config.get('HTTP.MESSAGE');
// "Hello!" (from default.json)

const port = config.get('HTTP.PORT');
// 4001 (from development.json, overrides 3001)
/* Windows */
set NODE_ENV=development& app.js

/* Linux */
NODE_ENV=development app.js

/* cross-platform */
cross-env NODE_ENV=development app.js

dotenv is a common solution for managing environment variables in node.js.

You can define an .env file for each environment you need. Then access them as node environment variables .

Depending on your use case, you could use environment variables to populate your config.json, or perhaps you won't need the config file at all and can just use the environment variables directly.

If I understand you correctly, you want to use Windows environment variables in your Node.js application so that the values are coming from the host operating system/environment itself rather than being hard-coded in a config file, which is a normal CICD requirement. Windows environment variables will be available wherever Node.js is installed by default. You don't need any additional packages. Both system and user environment variables will be available in Node.js process environment, process.env.whatever. For example:

const setting1 = process.env.setting1
const setting2 = process.env.setting2

However you cannot use them inside a .json file because they are static files, as @max-starling said. In this case I think a JavaScript config file makes more sense.

Main app.js

import config from './config'
console.log(config.setting1)

config.js

const somesetting = {
  setting1: process.env.setting1,
  setting2: process.env.setting2
}
module.exports = somesetting; 

Node docs: https://nodejs.org/api/process.html#process_process_env

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