Encrypt secrets in Node.js

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

Prerequisites

To get the most out of this guide, you'll need to:

1. Install

Get the Dotenvx Node.js SDK.

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

2. Encrypt

Encrypt your .env file.

$ npx dotenvx encrypt
◈ encrypted (.env) + local key (.env.keys)

3. Inject

Then inject your encrypted secrets at runtime.

index.js

require('@dotenvx/dotenvx').config()

console.log(`HELLO: ${process.env.HELLO}`)