How to Set Environment Variables for a Node.js App in cPanel
cPanel lets you set environment variables for your Node.js app directly in the Setup Node.js App tool, so database passwords, API keys and configuration stay out of your code. Your app reads them from process.env, just like anywhere else.
Why use environment variables
Hard-coding secrets and settings into your files is risky and inflexible. Environment variables keep them separate, so you can change configuration without editing code and avoid committing secrets — see the broader guide on managing secrets in Node.js.
Adding them in cPanel
- Open Setup Node.js App in cPanel.
- Click Edit on your application.
- Find the Environment variables section.
- Add each variable as a name and value (for example
DB_PASSWORDand its value). - Save, then restart the app.
Reading them in your app
const dbPassword = process.env.DB_PASSWORD;
The values you set in cPanel appear on process.env when the app runs.
Tips
- Restart after changes — new values load on restart; see restarting your app.
- Do not also commit a .env with the same secrets to your repository.
- Use different values for testing and production where relevant.
Frequently asked questions
Do I need a .env file on cPanel?
Not necessarily — the Setup Node.js App tool sets real environment variables for you. If your app also loads a .env file, keep it out of version control and be careful not to duplicate conflicting values.
My app doesn't see the new variable.
Restart the app after adding it — variables load when the app starts. Also confirm the variable name matches exactly what your code reads from process.env.
Are environment variables secure on shared hosting?
They keep secrets out of your code, which is a big improvement, but treat any shared environment with care. Use strong, unique credentials and rotate them if you suspect exposure.
Was this article helpful?