Environment configuration helps your app know important settings like where to find a database or what port to use. It keeps these details separate from your code so you can change them easily.
Environment configuration in Node.js
require('dotenv').config(); const port = process.env.PORT || 3000; const dbUrl = process.env.DATABASE_URL;
Use a .env file to store environment variables as key=value pairs.
Call require('dotenv').config() at the start of your app to load these variables into process.env.
# .env file PORT=4000 DATABASE_URL=mongodb://localhost:27017/mydb
require('dotenv').config();
console.log(process.env.PORT);const port = process.env.PORT || 3000; console.log(`Server runs on port ${port}`);
This Node.js program loads environment variables from a .env file and prints a greeting with the port number. If the variables are missing, it uses default values.
/* Create a file named .env with: PORT=5000 GREETING=Hello */ import dotenv from 'dotenv'; dotenv.config(); const port = process.env.PORT || 3000; const greeting = process.env.GREETING || 'Hi'; console.log(`${greeting}, server is running on port ${port}`);
Never commit your .env file to public code repositories to keep secrets safe.
You can use different .env files for development, testing, and production environments.
Environment variables are always strings, so convert them if you need numbers or booleans.
Environment configuration separates settings from code for flexibility and security.
Use a .env file and the dotenv package to load variables into process.env.
Always provide defaults and keep secrets out of your code.
Practice
Solution
Step 1: Understand environment configuration purpose
Environment configuration helps keep settings like API keys, database URLs, and ports separate from the code.Step 2: Recognize benefits
This separation allows changing settings without touching code and keeps secrets safe.Final Answer:
To separate settings like API keys and ports from the code for flexibility and security -> Option CQuick Check:
Environment configuration = separate settings [OK]
- Hardcoding secrets inside code
- Ignoring security by mixing config and code
- Not using environment variables
.env file using the dotenv package in Node.js?Solution
Step 1: Recall dotenv usage
The standard way to load environment variables is callingrequire('dotenv').config()at the start of your app.Step 2: Check other options
Options A, B, and D use incorrect method names or syntax not supported by dotenv.Final Answer:
require('dotenv').config() -> Option AQuick Check:
dotenv load method = config() [OK]
- Using wrong method names like load() or setup()
- Mixing import and require incorrectly
- Forgetting to call config()
.env file with PORT=4000, what will be the output?
require('dotenv').config();
console.log(process.env.PORT || 3000);Solution
Step 1: dotenv loads .env variables into process.env
The call torequire('dotenv').config()loadsPORT=4000intoprocess.env.PORT.Step 2: Evaluate console.log expression
The expressionprocess.env.PORT || 3000uses the loaded value 4000, so it prints 4000.Final Answer:
4000 -> Option AQuick Check:
Loaded PORT = 4000 [OK]
- Assuming default 3000 prints even if PORT is set
- Thinking process.env.PORT is undefined
- Expecting an error if .env exists
import dotenv from 'dotenv'; dotenv.config; console.log(process.env.DB_HOST);
Solution
Step 1: Check dotenv usage
The code callsdotenv.configwithout parentheses, so the config function is not executed.Step 2: Effect of missing parentheses
Without callingconfig(), environment variables from .env are not loaded, soprocess.env.DB_HOSTmay be undefined.Final Answer:
Missing parentheses after config method call -> Option BQuick Check:
Call config() with () to load env [OK]
- Forgetting () after config
- Assuming import syntax always fails
- Ignoring that process.env can be empty
Solution
Step 1: Identify secure storage method
Storing secrets like API keys in a.envfile keeps them out of code and version control.Step 2: Use dotenv to load and access keys
Usingdotenvloads the key intoprocess.env, which the app can safely use.Final Answer:
Store the API key in a .env file, load it with dotenv, and access it via process.env.API_KEY -> Option DQuick Check:
Secrets in .env, not code [OK]
- Hardcoding secrets in code files
- Committing secrets to public repos
- Using insecure files for secrets
Ready to prove it?
Pass the test and this pattern moves unknown
