[wp_ad_camp_1]
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,
1 | console.log(process.env.PATH); |
This will display the content of PATH.
[wp_ad_camp_2]
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.
1 | console.log(process.env.MY_ENV_TEST); |
[wp_ad_camp_3]