Encrypt secrets in Express
Learn how to encrypt your first .env file using Express and the Dotenvx Node.js SDK.
Prerequisites
To get the most out of this guide, you'll need to:
- Install dotenvx
- Create your account (optional)
1. Install
Get the Dotenvx Node.js SDK.
npm install @dotenvx/dotenvx
2. Encrypt env file
Encrypt your .env file.
$ terminal
npx dotenvx encrypt
3. Inject secrets
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}`)
})