snippet Thursday 25 April 2024

JavaScript Environment Variables (.env)

Author

The primary use of environment variables is to hide sensitive data or to provide different data based on the environment you are in

Key points

  1. Key Value Pairs
  2. Hide sensitive data
  3. Different data based on the environment
  4. Only works server side

Try it

in your terminal run two commands:

touch .env
npm install dotenv

Add your variable

Inside the .env file add in your variable

YOURUNIQUEKEYHERE=valueoverhere

Access it with JavaScript

Inside your js file require dotenv then access your variable

require('dotenv').config()
console.log(process.env.YOURUNIQUEKEYHERE)