Node.js
Environment Variables
Programming
Web Development
JavaScript

Read environment variables in Node.js

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Environment variables are essential in software development. They help in configuring and managing the behavior of applications without making changes to the codebase. In Node.js, environment variables are commonly used to store sensitive information or to adjust the application's behavior based on the deployment environment (development, testing, production, etc.). This article explores how to manage and securely handle environment variables in Node.js applications.

Accessing Environment Variables in Node.js

Node.js provides an inbuilt module called process which has an object env that stores all environment variables as key-value pairs. To access these values, you can use the following syntax:

javascript
const envVar = process.env.YOUR_ENVIRONMENT_VARIABLE;

For example, if you have an environment variable named PORT, you can access it in your Node.js code like this:

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

Here, process.env.PORT accesses the value of PORT from the environment, and if it's not defined, it defaults to 3000.

Setting Environment Variables

While you can set environment variables in your operating system, it’s often more practical to define these within the project. This can be done in various ways:

1. Using the Command Line

You can set an environment variable directly in the shell before running your Node.js application:

bash
PORT=4000 node app.js

2. Using .env Files

A popular approach is to use .env files which allow you to define all your environment variables in one file. These are not shipped with the application (typically ignored in version control) and can be different for each environment where the app is deployed.

To use .env files, you will need a package such as dotenv. This can be installed via npm:

bash
npm install dotenv

To utilize it, require dotenv and call config() at the top of your main file (e.g., app.js):

javascript
require('dotenv').config();

console.log(process.env.PORT);

Your .env file might look something like this:

 
PORT=4000
API_KEY=your_api_key
DATABASE_URL=your_database_url

Best Practices for Managing Environment Variables

  1. Don't Commit .env: Always add .env to your .gitignore file to avoid pushing sensitive keys to your version control system.
  2. Separation of Environments: Maintain different .env files for development, testing, and production. E.g., .env.production, .env.development.
  3. Secure Storage: Use services like AWS Secrets Manager or HashiCorp Vault for more secure handling of sensitive information in production environments.

Security Considerations

Managing environment variables also involves ensuring they are secure, as they often contain sensitive information like API keys and database passwords.

  1. Avoid logging environment variables in your application logs.
  2. Ensure only necessary personnel and systems have access to these variables.
  3. Regulate and rotate keys and passwords periodically to minimize risks if credentials are leaked.

Summary Table

FeatureDescription
Accessing VariablesUse process.env.VAR_NAME
Setting VariablesVia command line or .env files with dotenv.
Security Best PracticesDo not commit .env, separate environments, secure storage solutions.

Conclusion

Proper management of environment variables is crucial for maintaining the division between code and configuration. By following best practices, using packages like dotenv, and applying security strategies, you can effectively manage environment variables in Node.js projects ensuring configurations are both flexible and secure.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.