Skip to main content
D
Node.js
Node.js Environment Variables

Node.js Environment Variables

Node.js environment variables hide sensitive passwords and API keys.

Read Time
5 min read
Difficulty
Beginner
Last Updated
Jun 15, 2026
Version
v1.0.0

Introduction

Hardcoding passwords into your JavaScript files is dangerous because anyone viewing the code can see them. Developers use environment variables to store sensitive data securely on the server. The code reads these variables invisibly behind the scenes.

Syntax

Syntax
1/* this uses environment variables */
2require('dotenv').config();
3const dbPassword = process.env.DB_PASSWORD;

Key Points

  • The `process.env` object holds the variables.
  • The `dotenv` package loads variables from a `.env` file.
  • Never commit your `.env` file to GitHub.
  • They are essential for production server deployments.

Up Next

Continue your journey with the next topic.

Go to Authentication Basics