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
- First install dotenv
1 | npm install dotenv --save-dev |
Create .env file
1 | ABC_KEY = 1234qwer |
1 | // in server/index.js |
Remember in your .gitignore file to ignore .env file.
Set process.env variable in terminal
Second way is to directly set your variable in your terminal and access it in your node.
1 | # type this in your terminal |
Now you can access the variables in your node app by using process.env.ABC_KEY