To accomplish this, we will be using an npm package called dotenv.
Dotenv is a zero-dependency module that loads environment variables from a .env file into process.env
- Install the dotenv package
npm i dotenv
2. Require dotenv in the server file
require('dotenv').config();
3. Create a new file in your project called .env
4. In the .env file, paste all of your API keys
API_KEY=4423b234y329324b32
5. Access the API key with process.env
const api_key = process.env.API_KEY;
6. Create a .gitignore
file and add the .env
file to it
The code in the .gitignore
file:
.env
7. Upload to Github!
It’s recommended to include something that will help others understand what to do with .env
variables whether that be using the README, .env_sample, etc.