Node.js comes with built-in support for environment variables, which can be accessed through the env object within the global process object.
If you want to test this functionality, try creating your own environment variable directly in the Node REPL by adding a new property to the process.env object.
To manage environment variables in your Node application, it's recommended to use a tool like DotEnv.
DotEnv is a simple npm package that automatically loads environment variables from a .env file into the process.env object.
To start using DotEnv, install it by running the command: npm i dotenv
. Then, include and configure the package in your app like this: require('dotenv').config()
You can define multiple variables in the .env file. For instance, you might specify database-related variables such as:
DB_HOST=localhost
DB_USER=admin
DB_PASSWORD=password
No need to add quotation marks around strings when declaring variables - DotEnv handles this for you automatically.
Accessing your variables is straightforward! They are stored in the process.env object, making them accessible using the syntax process.env.KEY.
https://i.sstatic.net/TVNGs.png