Node-Hide environemnt variables in your app

Introduction

This is a question that I been struggle for a long time. However, I final figure it out. There are two ways that I recommend doing this using dotenv package or setting it manually.

dotenv package

Here’s my example repo: dotenv repo

  1. First install dotenv
1
npm install dotenv --save-dev

Create .env file

1
ABC_KEY = 1234qwer
1
2
3
// in server/index.js
require('dotenv').config()
console.log(process.env.ABC_KEY)

Remember in your .gitignore file to ignore .env file.

Screenshot 3

Set process.env variable in terminal

Second way is to directly set your variable in your terminal and access it in your node.

1
2
3
4
# type this in your terminal
export ABC_KEY=1234qwer # no space and no quotation
# for window replace export for set
unset ABC_KEY # unset variable

Hide Var
Hide Var 2

Now you can access the variables in your node app by using process.env.ABC_KEY