###### Table of Contents - [[#Passing env variables in the terminal]] - [[#Use a env file]] - [[#More info]] #### Passing env variables in the terminal You can pass environment variables on the terminal as part of your Node process. For instance, if you were running an Express app and wanted to pass in the port, you could do it like this: ```bash PORT=65534 node bin/www ``` To use the variable in your code, you would use the `process.env` object. ```js var port = process.env.PORT; ``` This could get become unwieldy: ```shell PORT=65534 DB_CONN="mongodb://react-cosmos-db:swQOhAsVjfHx3Q9VXh29T9U8xQNVGQ78lEQaL6yMNq3rOSA1WhUXHTOcmDf38Q8rg14NHtQLcUuMA==@react-cosmos-db.documents.azure.com:10255/?ssl=true&replicaSet=globaldb" SECRET_KEY="b6264fca-8adf-457f-a94f-5a4b0d1ca2b9" ``` #### Use a .env file Passing .env variables in the terminal doesn't scale, so a better way is to put them inside a file. Create a new file called `.env` in your project and put in the variables in there on different lines. ```node PORT=65534 DB_CONN="mongodb://react-cosmos-db:swQOhAsVjfHx3Q9VXh29T9U8xQNVGQ78lEQaL6yMNq3rOSA1WhUXHTOcmDf38Q8rg14NHtQLcUuMA==@react-cosmos-db.documents.azure.com:10255/?ssl=true&replicaSet=globaldb" SECRET_KEY="b6264fca-8adf-457f-a94f-5a4b0d1ca2b9" ``` The easiest way to read these values is to use the `dotenv` package from npm. ```bash npm install dotenv --save ``` Then just require that package in the project wherever you need to use environment variables. The `dotenv` package will pick up that file and load those settings into Node. ```js Use dotenv to read .env vars into Node require('dotenv').config(); var MongoClient = require('mongodb').MongoClient; // Reference .env vars off of the process.env object MongoClient.connect(process.env.DB_CONN, function(err, db) { if(!err) { console.log("We are connected"); } }); ``` ==PROTIP==: Don’t check `.env` files into Github. #### More info [Some website](https://test.com) ___ **Tags**: