In Node.js, you can read environment variables using the process.env object. This object provides access to all environment variables that are set when the Node.js process was started.

Here is an example of how to read an environment variable in Node.js:

const myVariable = process.env.MY_VARIABLE;
console.log(myVariable);


In this example, the MY_VARIABLE environment variable is read and stored in the myVariable variable. The value of myVariable is then logged to the console.

Note that environment variables are usually defined outside of the Node.js application, for example in a command line or a configuration file, depending on your operating system and use case.

To set an environment variable when running a Node.js application, you can use the command line flag -e followed by the variable and its value:
$ MY_VARIABLE=hello node myApp.js


In this example, the MY_VARIABLE environment variable is set to hello before running the myApp.js file with Node.js.