Node.js Environment Variables

Node.JS environment variables are values unique to the environment in which the program runs. They are helpful for storing sensitive information such as database credentials, API keys, and other application configuration parameters that should not be hard-coded. This post will go through using environment variables in a Node.js application.

Accessing Environment Variables

Environment variables are values unique to the environment in which the program runs. They are helpful for storing sensitive information such as database credentials, API keys, and other application configuration parameters that should not be hard-coded. This post will go through using environment variables in a Node.js application.

console.log(process.env.API_KEY);

Setting Environment Variables

There are numerous methods for configuring environment variables in a Node.js application. One method is to configure them in the terminal or command prompt before running the application. In Windows, for example, you may add an environment variable named API KEY to abc123 as follows:

set API_KEY=abc123

In Linux or MacOS, you can set the same variable like this:

export API_KEY=abc123

Another method is to use a software like dotenv, which allows you to save environment variables in a.env file. This file should be stored in the application’s root directory and should not be committed to version control. Here is an example of how to use dotenv:

require('dotenv').config();
console.log(process.env.API_KEY);

In this example, dotenv will seek for a file called.env in the application’s root directory and set the environment variables defined in the file.

Q&A

Q: How can I get the value of an environment variable?
A: In Node.js, you can get the value of an environment variable by using the process.env object and the variable’s key.

Q: How can I define environment variables?
A: There are numerous methods for configuring environment variables in a Node.js application. One method is to configure them in the terminal or command prompt before running the application. Another option is to use a software such as dotenv, which allows you to save environment variables in a.env file.

Exercises

  1. Create a new Node.js application and set an environment variable called API_KEY to abc123.
  2. Access the value of the API_KEY environment variable in the application.
  3. Create a new .env file in the root of the application and set the API_KEY variable to abc123.
  4. Use the dotenv package to load the environment variables from the .env file.

Answers

  1. In the terminal or command prompt, set the API_KEY variable to abc123 by running the command set API_KEY=abc123 on Windows or export API_KEY=abc123 on Linux or MacOS.
  2. In your application, access the value of the API_KEY variable by using the following code: console.log(process.env.API_KEY)
  3. Create a new file called .env in the root of the application and add the following line to it: API_KEY=abc123 4. To load the environment variables from the .env file, first install the dotenv package by running npm install dotenv in the terminal. Then, in your application, add the following code at the top of your main file: require('dotenv').config(); This will load the environment variables from the .env file and make them available through the process.env object.
Scroll to Top