I have a json file that includes the default convict config like so: (simplified for question)
{
"env": {
"doc": "The application environment.",
"format": ["production", "development", "test"],
"default": "development",
"env": "NODE_ENV"
},
"authentication": {
"jwtToken": {
"private": {
"doc": "jwt private token keep in .env file",
"format": "*",
"default": "KEEP TOKEN PRIVATE IN .ENV FILE",
"env": "JWT_PRIVATE_KEY",
"arg": "jwt_private_key",
"sensitive": true
},
"public": {
"doc": "jwt public token best to keep in .env file",
"format": "*",
"default": "BEST TO KEEP TOKEN PRIVATE IN .ENV FILE",
"env": "JWT_PUBLIC_KEY",
"arg": "jwt_public_key",
"sensitive": true
}
}
}
}
now i have an .env file with the following information: (these are obviously fake keys)
JWT_PRIVATE_KEY=-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEAtdrUNbxsSVTPBY2ySNE9QjkUJCszBASsKLwX15nxAFOLs/ah
/y+VArWIjVLD78b+DhPXgGWrsy8YQvudnqzIHcjHZZd+hwiikEOiJmKH8mVITozw
fyTbatSnQGJAHukCwgK+rol8vdG0XLf7E0GNtouOJV2yvToSbUFjGAtbP6lDK0v0
-----END RSA PRIVATE KEY-----;
JWT_PUBLIC_KEY=-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtdrUNbxsSVTPBY2ySNE9
-----END PUBLIC KEY-----;
Now i have a file that sets up convict like so:
where default_config
is the reference to the json file shown above.
import convict from "convict";
import default_config from "./defaults.config.json";
const config = convict(default_config);
const env = config.get('env');
config.loadFile('./configs/' + env + '.config.json');
config.validate({allowed: 'strict'});
export default config;
now i use my default config file in my index.ts like so:
import config from "./configs/configurator";
console.log(config.get('authentication.jwtToken.private'));
I expect the console.log
to print the RSA private key but instead i get the value from my defaults json eg: KEEP TOKEN PRIVATE IN .ENV FILE
why is this and how do i fix this?
Since in the docs of node-convict it says the following:
Environmental variables: If the variable specified by env has a value, it will overwrite the setting's default value. An environment variable may not be mapped to more than one setting.
Any help would be appreciated, if more information is needed let me know and i will edit the question.
turns out this is done only partially automatic...
You need to add to load the
.env
file to theprocess.env
variable first.I did this with the
dotenv
package like so: