Config module in Nodejs custome environment is not working - node.js

I am using Node JS 14.13.1 and config module version 3.3.6
I made a folder config and inside that, there are two files. config.json and custom-environment-variables.json
inside config.json, I have the following:
{
a:""
}
inside custom-environment-variables.json
{
a:"myENV"
}
What I see custom-environment-variables never override config.json.
I also set the system environment variable a from windows and also tried with cmd as:
set myEnv=myValue
I noticed that I need to restart my compute see process.env.myEnv in my node js
but even after restarting and run the app still the value of a is an empty string ( the default.json)
const config=require('config')
config.get('a') // returns empty string rather than shows myValue
Does anyone have this issue?
Thanks

Related

Nextjs process.env variables appending git installation path

So I'm on a Windows 10 machine running a trying to run a next.js app (I'm using gitbash). I have a file *.env file with all of my variables, then I also have the config files (next.config.js and config.js).
The *.env file looks something like this:
ATTRIBUTEONE=\somevalue
ATTRIBUTETWO=\somevalue
The config files look something like this:
const config = {
attributes: {
attributeOne: process.env.ATTRIBUTEONE,
attributeTwo: process.env.ATTRIBUTETWO,
(then more variables, you get the idea)
};
export default config;
Basically, I have problem in where when I run my project (I run it using node) all of the variables called through process.env are partially wrong - as in they append my git install path onto all of my variables, so for example attributeOne's value becomes C:\Users\User\AppData\Local\Programs\Git\somevalue, when it should just be \somevalue.
I have no idea why this is happening, any thoughts? Thanks.

NodeJS - config cannot not load custom environment variables

I am running config#1.30.0 and I am attempting to get settings from the environment variables using .\config\custom-environment-variables.json does not work. However, it reads from the .\config\default.json just fine.
.\config\custom-environment-variables.json
{
"key": "app_key"
}
.\config\default.json
{
"key": "defaultKey"
}
running
const config = require('config');
console.log(config.get('key'))
always prints
defaultKey
but prints nothing when I set the key property in config/default to an empty string. How can I resolve this?
What I have tried
Opened a new console anytime I set the environment variable using set app_key=newKey
Set the environment manually
The config file name relates to the NODE_ENV environment variable you use when starting node.
The purpose of the module is to have a config file for each type of environment you are deploying to test, staging, prod environments. Default takes over if nothing is set or a file can't be find
e.g. for test and staging environments you would have.
config/default.json
{
"key": "default_key"
}
config/test.json
{
"key": "test_key"
}
config/production.json
{
"key": "prod_key"
}
app.js
var config = require('config')
console.log(config.key)
Then if you run with a different NODE_ENV the same name as the file in the config directory you get the different keys
node app.js // output default_key
NODE_ENV=test node app.js // output test_key
NODE_ENV=production node app.js // output prod_key
You question references custom environment variables using the file config/custom-environment-variables.json This file will enable you to override a value in one of the files with a environment variable set when running node. This is useful when you can't commit the variable, such as database key but might want to access all your config in the same place.
e.g.
{
"key": "SECURE_DATABASE_KEY"
}
Then running the same program again with the new config file:
NODE_ENV=production node app.js // output prod_key
SECURE_DATABASE_KEY=asldfj40 NODE_ENV=production node app.js // output asldfj40
I ran into a similar problem and found that if I don't open a new terminal window and I restart the server in the same window where I run export some_secret=immasecret, then the app doesn't crash and the some_secret variable can be accessed. I'd previously been trying to access the variable while running node in another window.
This issue is with VSCODE Editors Integrated Terminal
We have also struggled a lot with this issue initially, but the issue is that you might be using the integrated terminal that comes with VSCODE there is an issue with that, please try to use some external terminals like cmder or cmd prompt that comes with windows you will get the output as you are expecting.
USE EXTERNAL TERMINAL OR CMD PROMPT to execute the code
A solution is custom-env nodejs module, it allows you to add different environment variables for different stages using the popular .env method. Example .env for dev environment and .env.staging for staging environment
Your files and codes are correct your cmd command is wrong
use this command
setx app_key NewKey
Attention!
If config/production.json
{
"key": "prod_key"
}
and config/local.json
{
"key": "local_key"
}
and
NODE_ENV=production node app.js
the Output is: local_key
If a local.json exist is NODE_ENV=production is ignored
Details s. config Wiki (it is very refined, unfortunately too few examples)

Issue with running node app using forever

So I have a node.js app in which I am using node-config-manager, to manage the various environments (development, staging, production).
I am trying to use forever to run this process in the background, however when I run forever start /path/to/main.js, it is telling me
Error: No file for this config - app.
The error is saying it's coming from line 9. When it first tries to add a configuration file, app.json.
1. import configManager from 'node-config-manager';
2. const options = {
3. configDir: './config',
4. env: 'develop',
5. camelCase: true
6. };
7. console.log(process.env.NODE_ENV);
8. configManager.init(options);
9. configManager.addConfig('app');
10. configManager.addConfig('logger');
11. configManager.addConfig('db');
export default configManager;
Inside my config folder I have three other folders, 'develop', 'staging', and 'production'.
Inside all of these folders I have three files, app.json, db.json, & logger.json.
So I am not sure what is causing the problem here. The config 'develop' does in fact exist, but there seems to be something wrong.
This app runs fine when I start it with nodemon, it's just forever thats causing problems.
Am I missing something with my understanding of how node-config-manager works? I thought all i had to do was change my NODE_ENV variable to the name of the folder inside my config directory and i'd be all set.
Thanks in advance for all the help.
the ./ directive for the config file looks at the relative path from the current working directory. forever is probably not being run from the working directory of the project, so it can't find the file.
if you project lives in /path/to then you need to provide that as the --workdingDir parameter to forever.
forever start /path/to/main.js --workingDir=/path/to/
that should take care of it

node-webkit karma temporary environment variable

I am developing an app using node-webkit and for testing i use
https://github.com/mllrsohn/grunt-node-webkit-builder
However, it requires the presence of an environment variable named NODEWEBKIT_BIN.
Since i want my project to be runnable for anyone, i would like to set this environment variable temporarily, for the duration of the testing/building phase.
I have tried to see if this is possible using package.json but it is not (and it is also not recommended).
I have also tried to use package.json scripts to call a custom config.sh script wherein i use 'export' to export the environment variable. This has no use since the env. variable is only available in the terminal opened to run the script, and is no longer there when Karma starts the node-webkit 'browser'.
i have googled for karma set environment variable but that yields nothing useful as well.
Basically, is this even possible? Or should i just demand that anyone cloning my project
sets this environment variable?
[edit] i am currently looking into setting process.env.NODEWEBKIT_BIN directly inside the karma.conf.js file on the first line, it looks like it might just work.
Awesome, this works, for anyone wondering:
// temp set environment variable for node-webkit
process.env.NODEWEBKIT_BIN = '/Applications/node-webkit.app/Contents/MacOS/node-webkit';
module.exports = function(config){
config.set({
basePath : './',
files : [
...
'App/scripts/tests/**/*.js'
...
],
frameworks: ['jasmine'],
browsers : ['NodeWebkit'],
singleRun: true
});
};

nconf not loading from file on heroku

I have switched over from using dotcloud to heroku. I am using nconf for my configuration. I have it setup that it first grabs from the environment variables, and if not there, then it grabs from the config.json file. On localhost this is working fine. For my build number, I store it in the config file, not in the environment variable, so that I can set it on push and then not have to change the environment.
app.coffee
nconf.argv().env().file file: "./config.json"
config.json
{
"APP_BUILD_NUMBER": "1.0.0"
}
If i run this locally or on dotcloud, nconf correctly passes 1.0.0 if I do
nconf.get("APP_BUILD_NUMBER")
but, on heroku, it returns undefined. If I do set it in the environment variables, then it does work. I am wondering what I am doing wrong.
Try removing the './' portion of the path:
nconf.argv().env().file file: "config.json"
If that doesn't work, try
nconf.argv().env().file file: __dirname + "/config.json"
I would recommend running heroku run bash then entering the Node REPL and trying multiple paths until you figure out what is different. Making a change and then waiting for a push is too tedious a debug cycle. I suspect your issue is around the path, or perhaps not unsetting the environment variable.

Resources