Environment Variables
Environment variables...
Listing environment variables
console.log(process.env);
The following will add the environment variable
API_KEY
before running theindex.js
file with the node command.API_KEY=the-value-of-the-key-goes-here node index.js
Setting 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 --save
Add a file to the project named
.env
with 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_ENV
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}Use the variable
console.log('App Name:', process.env.APP_NAME);