Environment variables contain a name and hold value similar to regular variables. Though, environment variables are supposed to remain on your local system and aren't visible when your code is in a different environment. So, for instance, pushing code to GitHub.
For the more technical definition: "env is a shell command for Unix and Unix-like operating systems. It is used to either print a list of environment variables or run another utility in an altered environment without having to modify the currently existing environment"[1].
And as for the file, "dotenv is a zero-dependency module that loads environment variables from a .env file into process.env. Storing configuration in the environment separate from code is based on The Twelve-Factor App methodology"[2].
GitHub has an open-sourced product named GitGuardian shield repository to help find exposed secrets in your commits. It detects for API keys, database credentials, certificates, and more. And yours truly received an email notification from them of a detected exposure. In this case, the public display of API keys. So let's solve this problem by using environment variables.
npm install dotenv
APISITE_ID=123456
APISITE_KEY=1234567890
const dotenv = require('dotenv');
dotenv.config();
const siteApiId = process.env.APISITE_ID;
const siteApiKey = process.env.APISITE_KEY;
Additional tip: In your README file, write a brief explanation to potential repo users about using a .env file to add their own API keys to view your project or the key directly in the js file. This sort of information can go under a section like "Prerequisites."
Katherine Delorme is UI/UX Designer with Frontend Development background. She loves creating designs that focus on solving problems more than following trends. Along with exploring how culture can impact design. She's most excited about inclusive design, and exploring how western and international design and usability contrast.
Her hobbies include learning the Japanese language, reading manga, watching anime and western cartoons, volunteering to teach the next generation of girls to code, hosting meetups, designing, and coding.