Why can't I access Heroku configuration in node? - node.js

Using the command line I set a configuration variable in Heroku
heroku config:set MONGOLAB_URI=...
In node I want to access that variable, so I'm using the following code
if (process.env.NODE_ENV === 'production') {
dbURI = process.env.MONGOLAB_URI;
}
On the terminal I run NODE_ENV=production nodemon and the error is that MONGOLAP_URL is undefined. However when I run heroku config MONGOLAP_URL is there.
I also logged process and process.env and heroku's variable is no where. I also created few other variables with heroku config:set to test it fully, and non of them show when I log process
The Heroku docs state the exact same thing as I have been doing.

You appear to be typing MONGOLAP_URL when the variable is actually MONGOLAB_URL.

Related

Heroku config vars/env vars not working (using node.js)

The following setup does not work:
Setting a config var either via the dashboard or via
heroku config:set TESTVAR=123
using
heroku config
shows the variable is set.
But then logging into the container using
heroku ps:exec
and logging out the env vars should show the variable set via config:set but it does not:
node -e "console.log(process.env)"
Also using
export
does not show the environment variable.
I tried defining the variable in app.json but to no avail:
"env": {
"TESTVAR":{
"required": true
},
According to the documentation this should work but unfortunately it does not. Any help is appreciated.
Try heroku run env instead.
According to the documentation: "The SSH session created by Heroku Exec will not have the config vars set as environment variables (i.e., env in a session will not list config vars set by heroku config:set)."
see this for more information
How to get environment variables in live Heroku dyno

db-migrate for node-postgres in next.js app not finding env variables on heroku

I'm trying to run db-migrate for postgres on heroku but cannot seem to figure out why it doesn't have the right env variables. Hoping for some guidance if someone else has encountered a similar problem.
I created a next.js app. Locally, I have a .env file with DATABASE_URL, etc. The app and migrations work just fine.
I pushed my app to heroku. The env variables are all defined in the heroku app config vars, so the app is working as expected. However, when I try to run db-migrate up or down, I get "Could not find database config file '/app/database.json'". I looked at the source and this only happens when process.env.DATABASE_URL is not defined. However, it is in my heroku app config vars, so I am confused as to why the migration cannot find the database URL. I thought the config vars would be pushed into node's process.env.
I am working around this by creating a new .env file in heroku every time I deploy but would prefer to fix this properly.

How to set some flags on my node build

When I build, test and deploy my node application from Codeship to Heroku I want to be able to set a release flag to true using a command line during the build. And in my code I want to do something like this....
if(config.release) load(liveConnection);
else load(debugConnection);
How can I achieve this? Is there some sort of package I install to run a build script which will transform my config file?
Instead of using a config file, you should use environment variables. For example:
heroku config:set NODE_ENV=production
Then, in node:
if (process.env.NODE_ENV === 'production') load(etc);
An even better way is to just provide connection information uniformly, through config files, like this:
heroku config:set CONNECTION_STRING=foo
Then in node:
load(process.env.CONNECTION_STRING);
That way, the environment is providing the config. Locally, you can start the app with a development string like CONNECTION_STRING=some_debug_string node server.js, or you can use a .env file to provide a whole set of them. More info here:
http://12factor.net/config

Is there a programmatic way to know a node.js app is running in Heroku?

Is there some variable or function I can call to know if a node.js application is running inside Heroku? Something like:
if (process.heroku)
console.log("I'm in Heroku!");
You can do this without setting custom environment variables. You can do it like this:
if (process.env._ && process.env._.indexOf("heroku") !== -1)
console.log("I'm in Heroku!");
This is possible because on a Heroku dyno the _ environment variable is set to /app/.heroku/node/bin/node.
You use for that usual environment variables.
Just set some variable on your heroku instance and check this:
process.env.HEROKU
On the heroku cli you would do:
heroku config:set HEROKU=true
You can also set it on the web interface, see heroku docs for more:
https://devcenter.heroku.com/articles/config-vars
On this specific case I've used if test -d /app; then npm run build; fi which can be used also inside npm-scripts.

NodeJS. Express 4. How to define environment

How can i define enviroment at NodeJS, Express4?
Don't work. Output error in console.
NODE_ENV=production node app.js
EDIT:
http://rghost.net/57634264/image.png
"NODE_ENV" is unknown command.
From the express 4 documentation:
settings
The following settings will alter how Express behaves:
env Environment mode, defaults to process.env.NODE_ENV (NODE_ENV environment variable) or "development"
Start your app with the command you posted:
NODE_ENV=production node app.js
If you've used express correctly (can't tell here as you've not posted code), you can access NODE_ENV through the app.get() method, which, in this context, will get the setting variable.
if (app.get('env') == 'production') {
// do something only production does
}
This is because windows CMD is a lot different than POSIX-like shells (used in linux and osx), which is what most of the tutorials and documentation for node userland is written for.
I recommend either using the Git Bash terminal that comes with git(link) to get a POSIX-like shell, or use the windows command to set the variable (takes two commands):
set NODE_ENV=production
node app.js

Resources