Node.js Environment Variable Debugging Guide
Debug Node.js environment variables across npm scripts, shells, Docker, PM2, services and frontend build boundaries. This Formalint reference is written for working developers, DBAs and support engineers who need a repeatable debugging path instead of a one-line snippet with no context.
Use the notes below as a practical review order: understand the input, capture evidence, make one small change and verify the result before moving to the next assumption.
When to use this page
Find where an environment variable disappears. It is most useful when a small validation or debugging mistake can create noisy tickets, misleading logs or hard-to-review production changes.
Practical workflow
| Step | What to confirm |
|---|---|
| Name the runtime | Server-side Node, frontend build tools and browser code see different variables. |
| Render config | For Docker Compose, inspect the rendered config before blaming application code. |
| Restart intentionally | Many process managers need a restart before new environment values appear. |
Environment evidence
node -e "console.log(process.version); console.log(process.env.NODE_ENV)"
npm run env
printenv | sort
docker compose configReview checklist
- Check spelling and case of variable names.
- Separate build-time variables from runtime variables.
- Do not expose secrets through frontend prefixes.
- Compare local shell, npm script and service manager environment.
- Mask values in screenshots and logs.
Common mistake
A variable can exist in your terminal and still be absent from the process that serves users.
Formalint is strongest when the page helps the developer decide what the tool cannot prove. Treat every formatter, regex and command as one layer of evidence, not the whole truth.
Frequently asked questions
Why does Vite need prefixed variables?
Frontend tools intentionally expose only selected variables to browser bundles.
Does changing .env update a running service?
Usually no. The process normally needs a reload or restart.
Related Formalint references
Continue with Node.js Runtime Guide, npm Dependency Debugging, Docker Env Files.