Encrypt a .env file in Nuxt with Dotenvx, commit it safely, and load its secrets at runtime
Prerequisites
Start with a Nuxt 4 app and Node.js. This guide deploys a standalone Node server.
Install
In your Nuxt project, install Dotenvx.
$ npm install @dotenvx/dotenvx
Encrypt
Start with a secret value in the .env file at your project root, like:
# .env
NUXT_HELLO="Secret"
Encrypt it with a single command.
$ npx dotenvx encrypt
The values become ciphertext and only your private key can unlock them.
# .env
DOTENV_PUBLIC_KEY="DOTENV-PUBLIC-KEY-3-SECP256K1-0220d830351410598be484f43a7b07097e09851f50426832876e8b5815a1752990"
NUXT_HELLO="DOTENV-CIPHERTEXT-3-SECP256K1-AES256GCM-0220d83035-BP8FZxI4vhmRmfTAmvZGnzBd6AoJG0ZSjKDLRbE4XpxaWBDD7PpxLMkB/2I0glwVkHXeIcpJwY13qtdhjySs+a9NT9myy5uzErUYb2Uc/1LDY886yF8SxslAxfiJzhZ5NV3qQDQ6Tg=="
Commit
Commit your encrypted .env files with your code. It's safe. Now you can securely share secrets through git.
$ git add .env
$ git commit -m "encrypt .env"
Ship
Declare your private runtime configuration, keeping your other options:
// nuxt.config.ts
export default defineNuxtConfig({
runtimeConfig: {
hello: ''
}
})
Nuxt maps NUXT_HELLO to runtimeConfig.hello at runtime. Keep the default empty so the build does not capture your secret.
Read it in a server route:
// server/api/hello.get.ts
export default defineEventHandler((event) => {
const config = useRuntimeConfig(event)
return { hello: config.hello }
})
Display the result:
<!-- app/app.vue -->
<script setup lang="ts">
const { data } = await useFetch('/api/hello')
</script>
<template>
<h1>Hello {{ data?.hello }}</h1>
</template>
NUXT_HELLO is a demonstration value. API responses reach the browser; use real secrets inside server routes without returning them.
Update these scripts in package.json, keeping your other scripts:
{
"scripts": {
"dev": "dotenvx run -- nuxt dev",
"build": "nuxt build --preset node-server",
"start": "dotenvx run -f .env.production --strict -- node .output/server/index.mjs"
}
}
Run your app:
$ npm run dev
Dotenvx loads and decrypts .env before Nuxt starts. Your server route reads it through useRuntimeConfig(event).
Visit http://localhost:3000 to see Hello Secret.
Public browser variables Alternative →
Share
Have teammates? Share access with Dotenvx Armor ⛨
Install the Armor feature before using Armor commands:
$ npx dotenvx install --add armor
Log in to Dotenvx.
$ npx dotenvx login
◉ logged in (username)
Create the Armored Key ⛨
$ npx dotenvx armor up
Invite your teammate.
$ npx dotenvx armor invite
⛨ invited [email protected] to team
Your teammate accepts the invitation, pulls the repository, installs dependencies, and logs in with dotenvx login. They can then run the app:
$ npm run dev
Visit http://localhost:3000 to see Hello Secret.
Share a private key Alternative →
Production
Create a .env.production file.
# .env.production
NUXT_HELLO="Production"
Encrypt it with an Enclaved Key ◊
$ npx dotenvx encrypt -f .env.production
When prompted, choose Armor, then Enclaved, select your deployment service, connection, and resource, and use DOTENV_PRIVATE_KEY_PRODUCTION as the private key name.
Commit .env.production.
$ git add -f .env.production
$ git commit -m "encrypt .env.production"
Install dependencies and build your app in your deployment build step:
$ npm install
$ npm run build
This server-rendered example only needs its secret at runtime, so the build script does not decrypt it.
Deploy .output, package.json, the dependencies needed to run Dotenvx, and the encrypted .env.production file to the selected resource. Keep .env.production at the project root and .env.keys on your local machine.
Set NODE_ENV to production and PORT to the port your host expects (3000 by default). From the project root, run:
$ npm start
Dotenvx loads and decrypts .env.production with your Enclaved Key ◊
Open your deployed app's URL to see Hello Production.
Static generation Alternative →
Deploy with a private key Alternative →
For serverless or edge deployments, follow your platform’s Nuxt deployment preset; the scripts above target a Node server.
Conclusion
You've encrypted your secrets, shared access with Armor, and loaded production secrets in Nuxt with an Enclaved Key. Your secrets now travel with your code.