Brendan
Brendan

Reputation: 852

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?

Upvotes: 0

Views: 1091

Answers (2)

Nick
Nick

Reputation: 749

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.

Upvotes: 0

user12251171
user12251171

Reputation:

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 ....

Upvotes: 1

Related Questions