Using .env file in a Node.js project

Last modified 1 months ago / Edit on Github

👉 We use dotenv.
👉 For VSCode, use DotENV extension.

Setting up

For a simple nodejs project,

npm init -y
npm i dotenv

# Create .env in this project
# MAKE SURE IGNORE IT IN GIT !!!

Usage in a Node.js

// At the top of the app
require('dotenv').config()

// Use
const db = require('db')
db.connect({
host: process.env.DB_HOST,
username: process.env.DB_USER,
password: process.env.DB_PASS
})

Use with js file

Use with a simple .js file? Using command line!

node -r dotenv/config your_script.js

# Custom path
node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/.env

# Single variable
DOTENV_CONFIG_<OPTION>=value node -r dotenv/config your_script.js

Use it in Github?

# In Github Action
https://github.com/dinhanhthi/<your-repo>/settings/secrets/actions

# In Github Codespaces
https://github.com/dinhanhthi/<your-repo>/settings/secrets/codespaces

# In Dependabot
https://github.com/dinhanhthi/<your-repo>/settings/secrets/dependabot

An example of using Github Action (key SECRET_TOKEN is already defined in /settings/...),

# Use a GitHub Actions secret variable in a bash shell
- name: Step 2 - GitHub Action if statement (true)
env:
WHO_TO_TRUST: $
if: env.WHO_TO_TRUST == 'TrustNo1'
run: echo "I know what the secret token is!"