Fantastic question Zain, thanks for noticing and posting this.
The environment variables for cloud servers or docker are loaded differently. You likely won’t want a file to travel with your code, as I mentioned. So then how do you get those? If running in Docker you can use Docker’s features to load ENV (several ways). If you are pushing directly to a cloud you can use the cloud provider’s features for setting these. Bother of these options (Docker and cloud providers) allow you to launch the app with something like
npm start or
node app.js while using their features to load the ENV’s.
Example — Docker compose files support loading ENV’s in the compose file or from a separate file. If you do neither, you can run the container in a cloud like Azure and set the ENVs in Azure, which then makes them available to the node process for you.
If interest is there — I’ll post the details of some these options in a future post.