Node.js: Environment Variables
Node.js Environment Variables [Best Practices]
Never commit commit
.envfiles 1.a For production, use a secrets manager likeAWS Secrets ManagerorHashiCorp Vault1.b Improve developer onboarding by providing a.env.examplefile with all required variables (without real values) and documentation in yourREADME.Multi-layered approach using dotenv with environment-specific files such as
.env.devand.env.prodNever use
process.envdirectly throughout your codebase. Instead create a centralized config object that validates required variables and provides proper type conversion
More
always hash the passwords i.e. use
bcrytlibraryalways encode the env variables i.e. encode it
base-64
https://deadsimplechat.com/blog/environment-variables-in-nodejs/
Secret Environment Variables [Best Practices]
tl;dr: even if your frontend app is very small, you are obliged to make a Backend API, otherwise you expose all your secret environment variables such as database passwords.
Secrets like API keys for secure services (e.g., database access, private APIs, admin credentials) should only live and run on the backend.
The best practice is to not access secrets like SECRET_API_KEY in frontend code at all.
If you include them in your frontend JavaScript, they’re visible to anyone in DevTools or by inspecting the bundle.
In short
backendprojects in nodejs+express: it's all good, go for itfrontendprojects (i.e. in react): use a proxy or backend api to securely call third party services
Frontend Projects and Secret Environment Variables [Best Practices]
Instead of exposing your secret to the client, use a Backend API or Proxy:
Your frontend calls your own API server (or serverless function).
The backend uses
SECRET_API_KEYsecurely to call the third-party service.The backend returns only the non-sensitive response to the frontend.
Bonus points
You control rate limits
You can filter/sanitize
You can rotate secrets
Example
Frontend code
Backend code - Express, Next.js API route, or serverless function
Common Beginner Mistakes
Make sure
.envis in the root folder.gitignorecontains.env,.env.development,.env.production,nodemon.jsonreadmedocuments the required environment variables in.env,nodemon.jsonand/or other fileswhen deploying on a Cloud Platforms-as-a-Service, you figure out if environment variables were included in the final javascript bundle, if not you must be set them up (prob manually) on the platform
JS Bundlers and Environment Variables
tl;dr: included in final bundle? it depends
Webpack
For webpack, you must use an additional plugin such as dotenv-webpack or DefinePlugin.
You manually define which environment variables to expose - which are then included in the final bundle.
Note: it's not super clear how this works, need to investigate
Vite
Vite has built-in .env support. It automatically loads .env, .env.development, etc.
Variables prefixed with VITE_ are exposed to the client - and then included in the final bundle.
Environment Variables Setup On Webpack
Webpackc.f. dedicated notes file on webpack
Environment Variables Setup On Vite
Vitec.f. dedicated notes file on vite
Last updated