Using the4 dotenv Nopde.js package to load variables from an .env file.
-
Create a file called
.envand place it in your project folder. -
Put your actual database credentials and API keys in your `.env``file:
DB_HOST=<DB_HOST> DB_DATABASE=<DB_DATABASE> DB_USERNAME=<DB_USERNAME> DB_PASSWORD=<DB_PASSWORD> API_SECRET=real_api_key
-
Create a second copy of your
.envfile and name it.env.sample. Use this file to provide instructions to the next programmer using your project code:DB_HOST=localhost DB_DATABASE=database DB_USERNAME=username DB_PASSWORD=password API_SECRET=api_key
-
Make sure your GitHub repo does not include the file named
.env. In your.gitignorefile add:.env
Note
I have excluded this step in this repo so you can view both the .env and .env.sample files.
-
Add
dotenvto your project:npm add dotenv npm i -
Start the Node.js app:
node app.js
This project uses Node.js.