DEV Community

Cover image for Environment Variables in Operating System and NodeJS
minoblue
minoblue

Posted on • Updated on

Environment Variables in Operating System and NodeJS

Environment variables are an important part of both operating systems and other applications. They provide a way to store configuration information that can be accessed by programs running on the system. In this article, we'll take a closer look at operating system environment variables and Node.js environment variables, and how they can be used to manage configuration information in your applications.

Operating System Environment Variables

Operating system environment variables are variables that are set at the operating system level and can be accessed by any program running on the system. These variables can store information such as the path to important files, the user's home directory, or the system's hostname.

One common use case for environment variables on the operating system level is to store sensitive information like passwords or API keys. By storing this information in an environment variable, it can be accessed by programs that need it without exposing the information in the code itself.

PATH - This is a variable that contains a list of directories where the operating system should look for executable files. For example, in Windows, the PATH variable might contain "C:\Windows\system32;" which is the directory where system files are stored.

USER - This variable contains the name of the currently logged-in user. For example, in Linux, the USER variable might contain "johndoe" if John Doe is currently logged in.

HOME - This variable contains the path to the user's home directory. For example, in macOS, the HOME variable might contain "/Users/johndoe" which is the path to John Doe's home directory.

JAVA_HOME - This variable is used to specify the path to the Java Development Kit (JDK) on the system. It is used by many Java-based applications and frameworks to find the necessary Java executables and libraries.

You can set environment variables in your operating system's environment. These environment variables will be available to all processes running on the machine, including Node.js processes.

On Linux/MacOS, you can set environment variables using the export command:

$ export MY_VAR=hello
Enter fullscreen mode Exit fullscreen mode

To get the value of a specific environment variable,use the $VAR_NAME syntax:

echo $VAR_NAME
Enter fullscreen mode Exit fullscreen mode

On Windows, you can set environment variables using the set command:

C:\> set MY_VAR=hello
Enter fullscreen mode Exit fullscreen mode

Node.js Environment Variables

Node.js environment variables are variables that are specific to a Node.js application and are set using the process.env object. These variables can be used to store configuration information for your application, such as database connection strings or API keys.

You can set environment variables for a Node.js project in several ways.

Command-line arguments:
You can pass environment variables as command-line arguments when starting your Node.js process.

$ MY_VAR=hello node app.js
Enter fullscreen mode Exit fullscreen mode

In this example, we're setting the environment variable MY_VAR to hello before running the node command to start the app.js script.

'.env' file:
You can create a .env file in the root directory of your Node.js project and define environment variables in it.

MY_VAR=hello
ANOTHER_VAR=world
Enter fullscreen mode Exit fullscreen mode

Then, you can use a package like 'dotenv' to load the environment variables defined in the '.env' file into your Node.js process.

require('dotenv').config()

console.log(process.env.MY_VAR) // Output: hello
console.log(process.env.ANOTHER_VAR) // Output: world
Enter fullscreen mode Exit fullscreen mode

Top comments (0)