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=abc123Another 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
- Create a new Node.js application and set an environment variable called
API_KEYtoabc123. - Access the value of the
API_KEYenvironment variable in the application. - Create a new
.envfile in the root of the application and set theAPI_KEYvariable toabc123. - Use the
dotenvpackage to load the environment variables from the.envfile.
Answers
- In the terminal or command prompt, set the
API_KEYvariable toabc123by running the commandset API_KEY=abc123on Windows orexport API_KEY=abc123on Linux or MacOS. - In your application, access the value of the
API_KEYvariable by using the following code:console.log(process.env.API_KEY) - Create a new file called
.envin the root of the application and add the following line to it:API_KEY=abc1234. To load the environment variables from the.envfile, first install thedotenvpackage by runningnpm install dotenvin 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.envfile and make them available through theprocess.envobject.