Netlify

Use dotenvx with Netlify

Find code examples for this guide on GitHub (Next.js and Astro).

Initial setup


npx create-next-app@latest --example hello-world .

[[plugins]]
  package = "@netlify/plugin-nextjs"

[build]
  command = "npm run build"
  publish = ".next"

npx netlify-cli@latest deploy --build --prod

Run dotenvx


npm install @dotenvx/dotenvx --save

Preload scripts with dotenvx so environment variables inject ahead of build, start, or dev.


"scripts": {
  "dotenvx": "dotenvx",
  "dev": "dotenvx run -- next dev --turbo",
  "build": "dotenvx run -- next build",
  "start": "dotenvx run -- next start"
}

Encrypt production


# .env.production
HELLO="production"

npm run dotenvx -- set HELLO production -f .env.production

Commit .env.production. Do not commit .env.keys.

Set decryption key


npx netlify-cli@latest env:set DOTENV_PRIVATE_KEY "your-private-key"
npx netlify-cli@latest env:set DOTENV_FILE ".env.production"
npx netlify-cli@latest deploy --build --prod

Your build injects env from the encrypted .env.production file.

markdown