Guide · 10 min read
Monorepo env vars with one config for pnpm, Turborepo, and Nx
You have three packages. Each one needs its own env vars. Someone added a fourth last sprint and the .env.example hasn't been updated since. A new hire joined Monday and still can't boot the web app because nobody remembers which keys belong where. The answer is always "ask Ana on Slack."
This page gives you a pattern for env vars in a monorepo. It has one config at the repo root, scoping per package, local overrides that don't touch your teammates' values, and task-runner setup for pnpm workspaces, Turborepo, and Nx. It also lists where each piece still breaks.
Why .env-per-package stops working in a monorepo
Three failure modes, in the order they hit a growing team.
1. Duplication drift
The same DATABASE_URL ends up in apps/api/.env, apps/worker/.env, and apps/admin/.env. Someone rotates the password in two of them. The third service crashes in staging the next morning because nobody remembered it also needed the new credential. The bug is not the duplication. The bug is that no single place holds the value, so a rotation can miss a copy.
2. Slow onboarding
A new engineer clones the repo and can't start the frontend because .env.local is gitignored and the template is stale. They ask in Slack. Someone pastes a JWT in a DM. By the time they're productive, the secret is in two Slack channels and one screenshot.
3. Cache poisoning
Turborepo and Nx cache task outputs by hashing inputs. Env vars count only when you declare them in turbo.json's env field or in Nx's inputs. The cache cannot see any other var the task reads. Say a task runs once without STRIPE_SECRET_KEY and the test passes because a mock kicked in. Turbo caches the success, and every later CI run replays that broken state. Two sprints later, someone clears the cache and finds the bug.
The three patterns teams reach for, and why each one breaks
First, the patterns that fail, and the reason each one fails.
Each pattern holds for 3 packages and 5 env vars and falls over at 8 packages and 30 env vars. Nobody can point to the moment it stopped working.
What a pattern that scales needs, with any tool
These four properties apply whether you build the setup yourself or adopt a tool. Without them, a monorepo fails for the reasons above.
1. One source of truth at the repo root.
A single file declares which packages exist, which env vars each one needs, and which environment (development, staging, production) they default to. Rotating a secret is one update; it propagates to every package that declared the key.
2. Per-package scoping.
Package A gets its keys. Package B can't see them. The frontend bundle can't accidentally include the Stripe secret because the frontend workspace never pulls it.
3. Per-user local overrides.
Your .env.local and your teammate's .env.local stay independent. You can point your local API at a staging URL without committing that choice into the shared config and breaking their build.
4. Task-runner awareness.
Whatever orchestrates your builds (Turborepo, Nx, or a Makefile) knows which env vars each task reads, so cache keys include them. An undeclared env var is a build error, not a silent cache hit.
With three of the four, one source of drift remains. You need all four.
How Envshed implements that pattern
Envshed is a hosted secrets manager. Its CLI reads one config file at your repo root, which maps each workspace to a project in your Envshed organization. envshed pull reads the map, fetches each project's secrets, and writes them where your code expects them.
This is the .envshed.json schema. Each package maps to { project, defaultEnv?, file?, org? }:
{
"org": "acme",
"project": "platform",
"defaultEnv": "development",
"workspaces": {
"apps/api": { "project": "platform-api" },
"apps/web": { "project": "platform-web", "defaultEnv": "development" },
"packages/auth": { "project": "platform-auth", "file": ".env.local" }
}
}Each workspace entry points to its own Envshed project. One envshed pull at the root pulls every workspace, one after another. envshed pull --workspace apps/api pulls a single package, for CI jobs that touch one service.
The file field overrides the default output path (./.env relative to the workspace). Use .env.local for the workspaces where Next.js or Vite expects it.
Envshed encrypts secret values at rest with AES-256-GCM. The server decrypts them for an authorized request and sends them over TLS, and envshed pull writes them to the paths above. Values are never logged in plaintext, each read lands in your audit log, and your config stays in the repo.
pnpm workspaces, step by step
pnpm's pnpm-workspace.yaml declares package globs. envshed init reads that file directly when you run it at the repo root.
Step 1. Detect and pick packages.
envshed init reads pnpm-workspace.yaml and lists the apps it finds under the configured globs, each with the project it connects to. Choose a project for the ones you want to map.
Step 2. Map each package to an Envshed project.
For each selected package, pick or create an Envshed project and an environment. The CLI writes the mapping into .envshed.json at the repo root.
Step 3. Commit the config.
.envshed.json has no secret values, only slugs and file paths, so it's safe to commit. That commit also onboards new hires. They clone the repo, run envshed pull, and the whole monorepo boots.
From there, pulling secrets takes one command:
envshed pull # pulls every workspace in parallel envshed pull --workspace apps/api # scopes the pull to one package envshed pull --env staging # same config, staging values
Wire it into your dev script so every local start picks up the latest values:
{
"scripts": {
"dev": "envshed pull && turbo run dev"
}
}The pull runs before every pnpm dev, so each local start reads the current values. CLI reference: envshed init, envshed workspace, envshed pull.
Turborepo, step by step
Turborepo caches task outputs, but it hashes only the env vars declared in turbo.json. An undeclared var can send a broken build to prod. The three steps below close that gap.
Step 1. Make the pull a task dependency.
Declare an envshed:pull task and add it to dependsOn. Set cache: false on the pull and on any long-running persistent task. The pull has to run every time to pick up rotated values, and Turbo never caches persistent tasks.
Step 2. Declare every env var in Turbo's env.
Turbo's cache hashing ignores anything your build reads via process.env that isn't listed here. An undeclared env var gives you "works on my machine, cached-broken on CI." Update this list whenever you add a var.
{
"tasks": {
"envshed:pull": {
"cache": false
},
"dev": {
"dependsOn": ["envshed:pull"],
"persistent": true,
"cache": false
},
"build": {
"dependsOn": ["envshed:pull", "^build"],
"env": ["DATABASE_URL", "STRIPE_SECRET_KEY", "NEXTAUTH_SECRET"],
"outputs": [".next/**", "!.next/cache/**"]
}
}
}Step 3. Wire it into CI with a service token.
In CI, create an Envshed service token scoped to one environment, store it as a CI secret, and run envshed pull at the start of the job. The token is the only Envshed value your CI config needs. Every other value rotates in Envshed.
- run: envshed pull
env:
ENVSHED_TOKEN: ${{ secrets.ENVSHED_CI_TOKEN }}Nx, step by step
Nx models every app and library as a separate project with its own project.json. The .envshed.json workspaces map lines up one to one, with one workspace entry per Nx project.
Step 1. Let each project read its own .env.
Nx automatically picks up apps/<name>/.env when executing a target in that project's cwd, so envshed pull writing to that path is all you need.
Step 2. Make the pull an Nx task dependency.
Add a local run-commands target per project and chain it with dependsOn on the targets that need secrets:
{
"name": "api",
"targets": {
"envshed": {
"executor": "nx:run-commands",
"options": {
"command": "envshed pull --workspace apps/api",
"cwd": "{workspaceRoot}"
}
},
"serve": {
"dependsOn": ["envshed"],
"executor": "@nx/node:node"
}
}
}Step 3. Declare env vars in namedInputs.
Nx only hashes env vars you declare, same rule as Turbo. Use namedInputs so the env set is reusable across targets:
{
"namedInputs": {
"runtime": [
{ "env": "DATABASE_URL" },
{ "env": "REDIS_URL" }
]
},
"targetDefaults": {
"build": { "inputs": ["default", "runtime"] }
}
}What Envshed does not solve
Three limits to know before you adopt it.
We don't run a sidecar or a proxy.
envshed pull writes values to .env files on disk, and any user or process that can read the file can read the values. envshed run skips the file, but the child process and its children still see every value. If you need a secret boundary in process memory only, Envshed does not provide one.
We don't replace CI-level secret stores for production deploys.
Vercel, AWS Parameter Store, 1Password Secrets Automation, and GitHub Actions Environments often hold production deploy secrets, and Envshed does not replace them. Envshed covers the local pull, onboarding, and the staging environment a product manager runs on a laptop. For CI, see the GitHub Actions guide.
We don't solve schema drift at the language level.
If your code reads process.env.DATABSE_URL with a typo, Envshed can't catch that. Use @t3-oss/env-core, Zod, or hand-rolled validators at each package's entrypoint. Envshed tells you which keys exist. Your runtime tells you whether they're the keys you expected.
Envshed runs no proxy you have to operate. envshed pull --format json --stdout exports every value, so you can leave at any time.
FAQ
Should .env files be committed in a monorepo?
No, and the answer is the same for a single repo and a monorepo. .env files contain secret values for one environment, so they belong in .gitignore. You commit .envshed.json, which holds only slugs and file paths, no values. If you use the .env.example pattern today, keep it as a list of required keys. Envshed does not replace that file.
How do I share a single env var like DATABASE_URL across packages without duplicating it?
Point both workspaces at the same Envshed project. apps/api and apps/worker both declare { "project": "platform-api" } in .envshed.json's workspaces map. One DATABASE_URL lives in one Envshed project, and envshed pull writes it into both workspaces. Rotate it once, and both packages get it on the next pull.
What about Next.js picking up .env.local automatically?
Supported. Set "file": ".env.local" in that workspace's config and envshed pull writes to the path Next.js expects. Per-user values that stay out of the shared config can live in a second local file next to it. Next.js merges the two with its documented precedence rules.
How do I run the whole repo with staging env in one command?
envshed pull --env staging && pnpm dev. The --env flag overrides the default environment for every workspace in one run. Use it for product reviews and bug reproduction.
Is this compatible with Docker Compose?
Yes. Run envshed pull before docker compose up, and Compose reads the .env file. You can also run envshed run -- docker compose up, which injects the values without a file.
How is this different from Doppler or 1Password Secrets Automation?
Doppler and 1Password Secrets Automation are general-purpose secrets managers. In Envshed, the root config, per-workspace pulls, and --workspace scoping are built into the CLI, with no scripts around it. Envshed has no policy-as-code and no multi-region HSMs, so a team that needs those has outgrown it.
See the pattern running
Clone the demo monorepo instead of copying snippets from this page. It has three packages, a Fastify API, a Next.js 15 web app, and a shared auth library. pnpm-workspace.yaml, turbo.json, and an .envshed.json with this guide's schema wire them together. After git clone, pnpm install, pnpm bootstrap, and pnpm dev start the dev servers, with no sign-up.
Public, MIT license. Fastify, Next.js 15, pnpm workspaces, and Turborepo. turbo.json lists the .env files, so changing one invalidates the cache.
Related reading
- Monorepo env vars, one config for every package, the short version, for readers who've already decided.
- Dotenv alternative, when .env files stop being enough, regardless of repo shape.
- Env vars in GitHub Actions, the CI-time pattern that complements this one.
- Envshed vs. Doppler, vs. 1Password Secrets, where each tool fits.
Replace the .env in every package
envshed pull at the root writes every workspace's .env. $5 per user per month.
A guide from Envshed.