Skip to content
dotenvxPublic

About

dotenvx drop-in replacement for @next/env

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

100 Commits

Folders and files

Repository files navigation

dotenvx

The secure dotenv–from the creator of dotenv.

dotenvx for Next.js

@dotenvx/next-env loads encrypted .env files during Next.js startup, so values are available when server modules first run.

Quickstart

In an existing Next.js app, add this npm override to your root package.json. It tells Next.js to use dotenvx for its @next/env dependency.

{
  "overrides": {
    "@next/env": "npm:@dotenvx/next-env@^3.0.0"
  }
}

Install the loader and confirm that npm ls shows @next/env as an alias of @dotenvx/next-env.

npm install @dotenvx/next-env@^3.0.0
npm ls @next/env

Add this value to .env at the app root.

# .env
HELLO=World

In an App Router project, create the following server route under app/ (or src/app/ if your app uses src/).

// app/api/hello/route.js
export function GET() {
  return new Response(`Hello ${process.env.HELLO}`)
}

Run npm run dev and visit http://localhost:3000/api/hello. The example returns Hello World. Next.js calls the loader automatically.

Encrypt and deploy

With the dotenvx CLI installed, encrypt the example from the project root. Install the CLI

dotenvx encrypt -f .env

Restart the dev server and visit the same route. The loader reads the matching private key from .env.keys. Commit the encrypted .env (use git add -f .env if Next.js ignores it) and keep .env.keys out of version control. Set the matching private key, such as DOTENV_PRIVATE_KEY, in your deployment environment wherever Next.js needs the decrypted values, including the build if it reads them there. Keep secrets in server-only variables. Next.js exposes NEXT_PUBLIC_* values to the browser.

Which files load?

Next.js uses the first value it finds in this order, relative to the app root.

  1. An existing process.env value
  2. .env.<mode>.local
  3. .env.local (skipped in test mode)
  4. .env.<mode>
  5. .env

The mode is development for the dev server, test when NODE_ENV=test, and production for production builds and starts. This integration uses Next.js file discovery even when DOTENV_FILE or DOTENV_PATH is set.

For a standalone tool that needs the same files, call the loader directly. The second argument selects development mode, as in next dev.

const { loadEnvConfig } = require('@dotenvx/next-env')
loadEnvConfig(process.cwd(), true)

Compatibility

The npm package includes prebuilt addons for macOS arm64/x64, Linux glibc arm64/x64, and Windows x64. Environment keys and .env.keys work without a CLI at runtime. Stored keys and Armor server-side decryption require a compatible dotenvx CLI on PATH with the sdk and corresponding custody features.

Next.js guide · Changelog

Design

Three widely used and proven primitives make up Dotenvx's design. Git, .env, and secp256k1.

We chose git because it is the best way to deliver digital goods. It is the container ship of the digital world. All code travels on its ships. Why not secrets? Why invent an inferior secrets delivery mechanism when the world class one is right there at your fingertips.

We chose .env because it is the open standard for loading secrets into code. It represents environment variables, the core primitive that all secrets end up injected into at runtime of code.

Lastly, we needed to choose an encryption algorithm. Encryption of the values inside the .env file would allow us to place those values with code, leveraging git's distribution advantages while still following the twelve-factor config – by separating the decryption key from the code (environment).

It was important that we choose a proven, simple, asymmetric standard with small keys. We chose secp256k1, battle-tested by Bitcoin for more than seventeen years. We made cryptography an opinionated choice so developers wouldn't have to. Encryption just worked.

The result is a system built entirely from primitives, developers already understand, and infrastructure they already use. Secrets can travel with code without becoming part of the code. Git distributes them, .env defines how apps consume them, and secp256k1 keeps their values unreadable. The kicker, all this works by setting a single private key on your infrastructure - no more risky async coordination of secrets, no more centralized downtime risk. Your secrets are always just there with your code, ready to be unlocked at runtime.

Read the whitepaper →

 

About

dotenvx drop-in replacement for @next/env

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors