Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Using dotenv for Environment Configuration in Node.js
📖 Scenario: You are building a Node.js application that needs to use secret keys and configuration values without hardcoding them in your code. To keep these values safe and easy to change, you will use the dotenv library to load environment variables from a file.
🎯 Goal: Learn how to set up a .env file, load it using dotenv, and access environment variables in your Node.js app.
📋 What You'll Learn
Create a .env file with specific environment variables
Install and import the dotenv package
Configure dotenv to load variables from the .env file
Access and use environment variables in your Node.js code
💡 Why This Matters
🌍 Real World
Many Node.js applications use environment variables to keep sensitive data like API keys and configuration separate from code. This helps keep secrets safe and makes it easy to change settings without editing code.
💼 Career
Understanding how to use dotenv and environment variables is essential for backend developers and anyone working with Node.js apps in real projects, especially when deploying to cloud or production environments.
Progress0 / 4 steps
1
Create a .env file with environment variables
Create a file named .env in your project root folder. Inside it, write these exact lines: API_KEY=12345abcde PORT=3000 DEBUG=true
Node.js
Hint
The .env file should have one variable per line in the format KEY=VALUE.
2
Install and import the dotenv package
In your Node.js project, install dotenv by running npm install dotenv. Then, in your main JavaScript file, add this line at the top: import dotenv from 'dotenv';
Node.js
Hint
Use ES module syntax to import dotenv as shown.
3
Configure dotenv to load environment variables
After importing dotenv, call dotenv.config() to load the variables from the .env file into process.env. Write this exact line: dotenv.config();
Node.js
Hint
Call dotenv.config() exactly to load the variables.
4
Access and use environment variables in your code
Write code to create three constants named apiKey, port, and debugMode. Assign them the values from process.env.API_KEY, process.env.PORT, and process.env.DEBUG respectively. Use this exact code: const apiKey = process.env.API_KEY; const port = process.env.PORT; const debugMode = process.env.DEBUG;
Node.js
Hint
Use process.env.VARIABLE_NAME to access each environment variable.
Practice
(1/5)
1. What is the main purpose of using dotenv in a Node.js project?
easy
A. To manage database connections automatically
B. To compile JavaScript code faster
C. To load environment variables from a file into process.env
D. To create HTTP servers easily
Solution
Step 1: Understand what dotenv does
dotenv reads a file (usually .env) and loads variables into process.env.
Step 2: Identify the main purpose
This allows your app to access secret or environment-specific settings safely without hardcoding them.
Final Answer:
To load environment variables from a file into process.env -> Option C
Quick Check:
dotenv loads env vars = C [OK]
Hint: dotenv loads .env variables into process.env [OK]
Common Mistakes:
Thinking dotenv compiles or runs code
Confusing dotenv with database or server tools
Expecting dotenv to manage app logic
2. Which of the following is the correct way to load environment variables using dotenv in a Node.js file?
easy
A. require('dotenv').config()
B. import dotenv from 'dotenv'; dotenv.load()
C. dotenv.setup()
D. require('dotenv').loadEnv()
Solution
Step 1: Recall the dotenv usage syntax
The official and common way to load variables is calling require('dotenv').config() at the start of your app.
Step 2: Check other options for correctness
The other options use incorrect method names or syntax not supported by dotenv.
Final Answer:
require('dotenv').config() -> Option A
Quick Check:
Use config() method to load dotenv [OK]
Hint: Use require('dotenv').config() to load env vars [OK]
Common Mistakes:
Using wrong method names like load or setup
Forgetting to call config() function
Trying to import dotenv without config call
3. Given the following code and .env file, what will be the output?
The code uses dotenv.config; without parentheses, so the function is not executed.
Step 2: Understand the effect of missing parentheses
Without calling config(), environment variables are not loaded into process.env, so SECRET_KEY remains undefined.
Final Answer:
Missing parentheses after config function call -> Option A
Quick Check:
Call config() with () to load env vars [OK]
Hint: Always call config() with parentheses to load env [OK]
Common Mistakes:
Forgetting parentheses on config function
Assuming require auto-executes config
Ignoring missing .env file or variable
5. You want to use dotenv to load different environment variables for development and production. Your .env file has NODE_ENV=development and API_URL=http://localhost:3000. You also have a .env.production file with NODE_ENV=production and API_URL=https://api.example.com. How can you load the correct file based on the environment?
hard
A. Use dotenv.loadEnv(process.env.NODE_ENV) to auto-load
B. Call require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` }) after setting NODE_ENV
C. Rename .env.production to .env manually before running
D. Call require('dotenv').config() only once without options
Solution
Step 1: Understand dotenv supports custom paths
dotenv's config function accepts a path option to specify which file to load.
Step 2: Use NODE_ENV to select the file dynamically
By calling require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` }), you load .env.development or .env.production based on the environment.
Final Answer:
Call require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` }) after setting NODE_ENV -> Option B
Quick Check:
Use config({ path }) to load env files by environment [OK]
Hint: Use config({ path: `.env.${NODE_ENV}` }) to load env files [OK]
Common Mistakes:
Not specifying path option for different env files
Manually renaming files instead of dynamic loading