Encrypt secrets in Express

Learn how to encrypt your first .env file using Express and the Dotenvx Node.js SDK.

1. Install

Get the Dotenvx Node.js SDK.

$ npm install @dotenvx/dotenvx
# or
# bun add @dotenvx/dotenvx
# pnpm add @dotenvx/dotenvx

2. Encrypt

Encrypt your .env file.

$ npx dotenvx encrypt
◈ encrypted (.env)

3. Inject

Then inject your encrypted secrets at runtime.

index.js

require('@dotenvx/dotenvx').config()
const express = require('express')

const app = express()
app.use(express.json())

app.get('/', (req, res) => {
  res.json({
    HELLO: process.env.HELLO
  })
})

const port = process.env.PORT || 3000
app.listen(port, () => {
  console.log(`Server running on http://localhost:${port}`)
})