Node.js demo project that shows how dotenv helps keep secrets like API keys out of your source code.
This project was created for a JSCRIPT 330 Supplemental Topic Presentation. The demo introduces dotenv, a small but important JavaScript library used in real-world development. The README.md is a bit of overkill but trying to practice my technical writing.
dotenv loads environment variables from a .env file into process.env, which allows applications to use secrets without hardcoding them directly into JavaScript files.
Hardcoding secrets such as API keys, passwords, or tokens inside JavaScript files is risky. If code is uploaded to GitHub, those secrets can be exposed.
dotenv solves this problem by:
- keeping secrets in a separate
.envfile, - loading them into the application when it starts,
- and allowing developers to safely access them using
process.env.
- Creating a Node.js project
- Installing dotenv
- Creating a
.envfile - Reading a value with
process.env.API_KEY - Using
.gitignoreso.envstays out of GitHub
index.js- main JavaScript demo.gitignore- excludes.envandnode_modulespackage.json- project configuration and dependency listREADME.md- repository overview and instructionsLICENSE- simple MIT license filescreenshots/- visual demo images you can use in slides or the repo
/home/gdevops/Projects/JSCRIPT330B/jscript-330-sup-topichttps://github.com/gnaxc/jscript-330-sup-topic
- Node.js installed
- npm installed
- RHEL 10 desktop
Think of .env like a private note for your program.
Your JavaScript code can read the note when it runs, but the note itself does not belong in public source code.
That means:
- the app still gets the value it needs,
- but GitHub does not expose that value,
- which is safer and more professional.
The real .env file should never be uploaded to GitHub.
This project uses .gitignore to prevent that.
- "Hardcoding secrets in JavaScript is a security risk."
- "dotenv loads values from a
.envfile intoprocess.env." - "The code reads the secret without storing it directly in
index.js." - "
.gitignorehelps keep the real.envfile out of GitHub."
See the screenshots/ folder for demo images you can use in your slides, README, or repo.