Access environment variable in Node.js

This short post shows how to access environment variables in Node.js.

Process object

Node.js has a global object process that can readily be used in your JavaScript application without explicitly using require.

Environment Variable

With the process  object, the env property can be used to access an environment variable. For example,

This will display the content of PATH.

Example Usage

Let’s try creating a custom environment variable named MY_ENV_TEST with value of “This is my env variable”.

In your terminal (Windows), create the variable.

In the same terminal, run the following JavaScript.