nconf not loading from file on heroku - node.js

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.

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)

Using environment variables in Node

I have been trying to get a streamline way of having different environment variables for local and production web apps, but I haven't come across the "ideal" solution yet.
There's the option of having a config.js file like so:
//config.js
{
"secretKey": "SDFDASFFSFD",
"facebook": {
"clientID": "EFGFDGBGDGFS",
"clientSecret": "EGDFNHFG"
}
}
And accessing via ES6 imports
Or using .env files like so:
SOME_KEY=someValue
HELLO=world
FACEBOOK_SECRET=435SDFSF5DZVD7S
And accessing the variables via process.env in the code using dotenv.
Obviously no matter what route you go down, the file will need to be omitted from version control which is fine. Each of these ways are great, but they only seem to work well for local development.
So how do you then have a separate file for a production environment? The dotenv docs say they strongly recommend against a .local.env and .prod.env situation.
Also, how is best to push to a remote server? I have my own server with Gulp tasks which run on a Git post-receive hook. How is best to pass up the production environment variables to here?
Thanks
You could have own config file for each environment:
- environments
- index.js
- deveplopment.json
- staging.json
- production.json
To use appropriate config file, run the app with required NODE_ENV:
NODE_ENV=production node index
In environments/index.js determinate the current NODE_ENV and use config:
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
module.exports = require('./' + process.env.NODE_ENV);
If config file doesn't include secret info (apiKeys, etc), it can be pushed to repo. Otherwise add it to .gitignore and use environment variables on the server.
Note:
For advanced configuration use such packages as nconf.
It allows to create hierarchical node.js configuration with files, environment variables, command-line arguments, and atomic object merging.
Have you thought about having a keys.js file with a .gitignore on it?
I've used this in the past to use module.exports{} to get my variables usable but not going to version control for the security side of things!

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
});
};

Resources