dotenvx

Quickstart ⚡️

Manage secrets with dotenvx.

Setup

Install dotenvx.

# install with Homebrew and then use the dotenvx command
brew install dotenvx/brew/dotenvx
dotenvx help

Install the necessary web server libraries in the language of your choice.

npm install express --save

Create a simple Hello World program.

// index.js
const express = require('express')
const app = express()
const PORT = process.env.PORT || 3000

app.get('/', (req, res) => {
  res.send(`Hello ${process.env.HELLO || ''}`)
})

app.listen(PORT, () => {
  console.log(`Server running on port:${PORT}`)
})

Run it.

$ node index.js
Server running on port:3000
# http://localhost:3000
# Hello undefined

Run Command

Run it with dotenvx run --.

$ dotenvx run -- node index.js
[dotenvx][warn] ENOENT: no such file or directory, open '/path/to/.env'
Server running on port:3000
# http://localhost:3000
# Hello undefined

dotenvx is more helpful than dotenv. It warns you of a missing .env file.

Create the .env file.

# .env
JELLO="World"

Run it again.

$ dotenvx run -- node index.js
[dotenvx][info] loading env (1) from .env
Server running on port:3000
# http://localhost:3000
# Hello undefined

Hrm, still not saying Hello World. Pass the --debug flag to to see what's going wrong.

$ dotenvx run --debug -- node index.js
[dotenvx][verbose] laoding env from /path/to/.env
[dotenvx][debug] reading env from /path/to/.env
[dotenvx][debug] parsing env from /path/to/.env
[dotenvx][debug] {"JELLO":"World"}

# Oops, HELLO not JELLO ^^

It's easier to debug with dotenvx than dotenv.

Fix your .env file.

# .env
HELLO="World"

Run it one last time.

$ dotenvx run -- node index.js
[dotenvx][info] loading env (1) from .env
Server running on port:3000
# http://localhost:3000
# Hello World

🎉 It worked!