0
0
ExpressHow-ToBeginner · 3 min read

How to Use dotenv with Express for Environment Variables

To use dotenv with express, first install dotenv and create a .env file with your variables. Then, require and configure dotenv at the very start of your main server file before accessing any environment variables with process.env.
📐

Syntax

Here is the basic syntax to use dotenv in an Express app:

  • require('dotenv').config(); loads variables from .env into process.env.
  • Access variables with process.env.VARIABLE_NAME.
  • Place require('dotenv').config(); at the very top of your main file.
javascript
require('dotenv').config();

const express = require('express');
const app = express();

const port = process.env.PORT || 3000;

app.get('/', (req, res) => {
  res.send(`Server running on port ${port}`);
});

app.listen(port, () => {
  console.log(`App listening on port ${port}`);
});
💻

Example

This example shows a simple Express server using dotenv to load a custom port from a .env file.

The server responds with the port number it is running on.

javascript
/* .env file content (create this in your project root):
PORT=4000
*/

// index.js
require('dotenv').config();
const express = require('express');
const app = express();

const port = process.env.PORT || 3000;

app.get('/', (req, res) => {
  res.send(`Server running on port ${port}`);
});

app.listen(port, () => {
  console.log(`App listening on port ${port}`);
});
Output
App listening on port 4000
⚠️

Common Pitfalls

Common mistakes when using dotenv with Express include:

  • Not calling require('dotenv').config() at the very top of the main file, so environment variables are undefined.
  • Forgetting to create or properly name the .env file.
  • Not restarting the server after changing the .env file.
  • Committing the .env file to public repositories, exposing secrets.
javascript
/* Wrong way: dotenv config called after using env variables */

const express = require('express');
const app = express();

const port = process.env.PORT || 3000; // PORT is undefined here if dotenv not loaded

require('dotenv').config(); // Too late

app.listen(port, () => {
  console.log(`App listening on port ${port}`);
});

/* Right way: dotenv config at the top */

require('dotenv').config();
const express = require('express');
const app = express();

const port = process.env.PORT || 3000;

app.listen(port, () => {
  console.log(`App listening on port ${port}`);
});
📊

Quick Reference

  • Install dotenv: npm install dotenv
  • Create .env file: Store key=value pairs, e.g., PORT=4000
  • Load dotenv: require('dotenv').config(); at the top of your main file
  • Use variables: Access with process.env.VARIABLE_NAME
  • Security: Add .env to .gitignore to keep secrets safe

Key Takeaways

Always call require('dotenv').config() at the very start of your main Express file.
Store environment variables in a .env file and never commit it to public repos.
Access variables using process.env.VARIABLE_NAME in your Express app.
Restart your server after changing the .env file to apply updates.
Use a default value in code to avoid errors if environment variables are missing.