0

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?

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

So using heroku config:set... in my .env file will allow that environment variable, in my .env file, to be used in production mode?
No, not in your .env file. I'm not very familiar with Heroku but I assume you'd use that command in a terminal and Heroku would use that on their server.
0

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.

1 Comment

So if the .env file is only read in local development mode, how can the production mode of my application work if the heroku config:set MONGODB_URI... is in my .env file? Once I deploy to Heroku, how can my application use the MONGODB_URI environment variable since my application would now be in production mode?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.