Heroku config vars/env vars not working (using node.js) - 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

Related

Node app deployed on Heroku only has a few env vars, and none of the ones specified by config

I am trying to deploy a React app on heroku and even though I have multiple environment variables set in heroku's config, none of them are appearing at run time. I added a console.log(process.env) to my app and the output was this:
FAST_REFRESH: true
NODE_ENV: "production"
PUBLIC_URL: ""
WDS_SOCKET_HOST: undefined
WDS_SOCKET_PATH: undefined
WDS_SOCKET_PORT: undefined
My heroku config is this:
BACKEND_URL: https://lz-comparables-manager-be-stg.herokuapp.com/
NODE_ENV: staging
NODE_OPTIONS: --max_old_space_size=1024
SKIP_PREFLIGHT_CHECK: true
How come none of those variables (or any of the other normal env vars of a Node app) are showing up in the app?
The prefix REACT_APP_ is necessary when accessing env variables, try
{process.env.REACT_APP_SOMETHING}
Also should you write prefix when define.
https://create-react-app.dev/docs/adding-custom-environment-variables/

Difference in environment variable usage between development and production modes?

I'm learning how to deploy a simple backend node.js project to production, in my case heroku. I came across this note regarding environment variables in a .env file:
The environment variables defined in dotenv will only be used when the backend is not in production mode, i.e. Heroku.
We defined the environment variables for development in file .env, but the environment variable that defines the database URL in production should be set to Heroku with the heroku config:set command:
heroku config:set MONGODB_URI=mongodb+srv:...
How can my .env file use the heroku config:set MONGODB_URI=mongodb+srv... environment variable, if using heroku would mean that my backend is in production mode? The first sentence states that environment variables are only used for development mode.
What am I understanding wrong? Are environment variables used in both development mode and production mode, and the wording of the note I read was wrong?
I think it's saying that environment variables in .env will be used when you're in development (not Heroku) and that if you do want to use your environment variables in Heroku that you need to do it through heroku config:set ....
What am I understanding wrong? Are environment variables used in both development mode and production mode, and the wording of the note I read was wrong?
Environment variables are used in all scenarios, but only in local development mode they are read from a file.
dotenv is a package that allows reading environment variables from a file.
In production mode, what Heroku is essentially doing is this, for example:
PORT=9999 node index.js
Try it, you can access this variable inside Node by using:
console.log(process.env.PORT);
// 9999
Environment variables are just global variables available to the entire process.

Why can't I access Heroku configuration in node?

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.

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.

Resources