NodeJS Environment Variables
By gobrain
Dec 17th, 2024
When developing a Nodejs application, you typically work on it locally on your computer, as do your colleagues. Once development and testing is complete, the application is eventually deployed to a server.
During this workflow, each system where the application runs is referred to as an environment, and environment variables provide essential data to ensure the code functions seamlessly across different environments.
What Are Environment Variables?
Environment variables are key-value pairs used to store configuration settings outside your application's code. They help your application adjust its behavior depending on the environment it runs in, such as a local development machine, a cloud server, or a Docker container.
Using environment variables in your NodeJS application is important because of:
- Security: Keeping sensitive data like API keys or database credentials out of your codebase minimizes security risks.
- Portability: Configuration changes don't require code modifications; you only need to update environment variables.
- Flexibility: Different environments (e.g., dev vs. prod) can have specific configurations.
Below are common use cases for environment variables:
- API Keys and Secrets: Storing keys for third-party services, like Google APIs or Stripe.
- Database Configurations: Managing database URLs, usernames, passwords, and ports.
- Service Endpoints: Switching between development and production servers.
- Feature Flags: Toggling features on or off without code changes. Application Settings: Controlling logging levels or debug modes.
How To Use Environment Variables in Nodejs
Node.js provides easy access to environment variables using the process.env object. This object contains all the current environment variables as key-value pairs.
const port = process.env.PORT
console.log(`Server running on port ${port}`);
When you run the code above, you will see port
is undefined because you did not set the PORT environment variable. You can achieve this in two ways:
- Using your terminal
- Using .env File
How To Set Environment Variables In Terminal
To set an environment variable to your nodejs application from your command line, simply define a variable, followed by the equal sign and the value before you run the application.
PORT=8080 node app.js
This will set 8080 to the PORT variable so that you can see the console message Server running on port 8080
without being PORT undefined.
In this way, you can easily set multiple variables as well.
PORT=8080 NODE_ENV=development node app.js
Using .env File
Instead of setting environment variables manually each time, you can use a .env file to store them. A .env file is a simple text file where you define all your environment variables, and it is loaded into your application using libraries like dotenv.
npm install dotenv
Create a .env file in your project root:
# .env
PORT=5000
DATABASE_URL=postgres://user:password@host:port/database
At the top of your Node.js entry file (e.g., index.js or app.js), load the .env file:
require('dotenv').config();
const PORT = process.env.PORT;
console.log(`Application running on port ${PORT}`);
This method is highly convenient and helps keep your application’s environment variables organized.
Conclusion
Environment variables are a powerful tool that every developer should master, particularly when working with Node.js applications. They allow you to manage sensitive data, maintain flexible configurations, and adapt to different environments without altering the code itself.