“For every minute spent in organizing, an hour is earned,”

–Benjamin Franklin

Environment Variables 🌳

If you care about making your app run on any computer or cloud (aka your environments), then you should use environment variables

Node.js Everywhere with Environment Variables!

A server's functions can be customized not just by implementation code or client requests, but also via configuration. For instance:

We can hard-code these environment variables, but it's better to put them in a configuration file that our code can read and we can maintain easily.

Ideally, you'll have one set of env. variables per environment, e.g. one for dev and one for prod.

<aside> 😅 No more hard-coding server port number! We were hard-coding 4000 in the server.js for our local machine, but it's definitely not gonna be 4000 when it's deployed later to the cloud!

</aside>

c3.1 Creating an env. variables file

We'll use a library called dotenv to manage our local env. variables.

For prod (i.e. when your app is deployed to the cloud, see notes at end of chapter)

$ npm install dotenv

Create a .env file on the root and put these values.

.env

NODE_ENV=development
PORT=4000
# Database connection goes here
# API keys go here

<aside> ⚠️ An env file is not supposed to be pushed to your public repo, since they are specific to your local environment and can contain secrets like passwords, API keys. Keep it secure! 🔐

</aside>