Environment Variables
Environment variables...
Listing environment variables
console.log(process.env);The following will add the environment variable
API_KEYbefore running theindex.jsfile with the node command.API_KEY=the-value-of-the-key-goes-here node index.jsSetting environment variables in a node script
process.env.APP_NAME = 'Dabble Lab Example App';
Using .env files
You can load environment variable from a file named .env using the dotenv npm module
Install the npm package
npm install dotenv --saveAdd a file to the project named
.envwith each variable and value on its own lineAPI_KEY=the-value-of-the-key-goes-here
APP_NAME = 'Dabble Lab Example App'Add the following require command before using the environment variable
require('dotenv').config();Check for the
NODE_ENVif (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}Use the variable
console.log('App Name:', process.env.APP_NAME);