Software Development

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.








Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.

You Might Also Like