Fri Jun 18 2021
// install dotenv with npm install dotenv // use it in the main file of your application import dotenv from 'dotenv'; dotenv.configure() console.log(process.env.YOUR_VARIABLE) // your_variable_value // .env file YOUR_VARIABLE=your_variable_value // .gitignore file .env
Sometimes you'll need to store some configuration variables that shouldn't be placed directly in the code because of security reasons (you might store your DB password here) or because they need to change depending on the deployment stage (the url endpoint of the DB might be different). One approach to solve this is to create a .env file containing this variables and deploy it manually. In order to easily read and parse this file we can use the dotenv library. You'll need to install it and execute its "configure" method. Once that's done, you'll be able to read the variables normally from the process.env object.
Note that it is highly recommended to include this .env file in your .gitignore so it is not accidentally pushed to a public repository, exposing all your secrets.