Environment variables are supported out of the box with Node and are accessible via the env object (which is a property of the process global object.)
To see this in action, you can create your own environment variable right in the Node REPL by appending a variable to the process.env object directly.
To create environment variables in your Node app, you will probably want to use a package like DotEnv.
DotEnv is a lightweight npm package that automatically loads environment variables from a .env file into the process.env object.
To use DotEnv, first install it using the command: npm i dotenv Then in your app, require and configure the package like this: require('dotenv').config()
You can declare multiple variables in the .env file. For example, you could set database-related environment variables like this:
DB_HOST=localhost
DB_USER=admin
DB_PASSWORD=password
There is no need to wrap strings in quotation marks. DotEnv does this automatically for you.
Accessing your variables is super easy! They are attached to the process.env object, so you can access them using the pattern process.env.KEY.