-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Global Environment Variables from Root or via a "Package" #3928
Comments
Thanks for filing @anricoj1. Just to make sure I understand the request: are you looking for more guidance on how to load environment variables in your monorepo? Or are you thinking that Turborepo can do something automatically when you do |
@mehulkar Correct I was hoping running I think adding more guidance in the docs on managing environment variables across your turborepo would be very helpful. I'm using my current issue as an example for managing environment variables throughout my turbo repo. I have a Is it better practice to:
It would be great if turbo could handle this out of the box or at least provide an opt in via As the template already provides you with example |
+1: It's also not clear in the with-prisma example that the env solution there won't work on windows (unless I've done something horribly wrong). The best alternative seems to be adding If there is a better way it doesn't seem to be documented, and if there isn't it seems like some kind of automatic solution would be a valuable improvement. |
Does #4406 go far enough for you? I'd be happy to review a PR for where you think some extra material could go if you want to start one up! I'm guessing somewhere in the Monorepo Handbook is a good idea. |
Ah, didn't realise that had been merged at the same time I was having the problem.. It looks like the |
Just wanted to share my thoughts. I think having some common environment variables to the whole monorepo is useful, however, as you know, in a monorepo with multiple applications where we can specify an app and a vercel project its more useful to have environment variables local to the application itself. I've tried to add Is there a solution I can use to avoid going down the path of
|
@andrevenancio could this package help? @dotenv-run/cli /workspace
apps
frontend1
.env.local # API_USERS=http://localhost:3001/users
src/
.env.dev # API_BASE=https://dotenv-run.dev
.env.prod # API_BASE=https://dotenv-run.app
.env # API_USERS=$API_BASE/api/v1/users API_AUTH=https://$API_BASE/auth
package.json
turbo.json $> cd /workspace/apps/frontend1
$> NODE_ENV=dev dotenv-run -- bash -c 'printf "✨API_USERS $API_USERS\n✨ API_AUTH $API_AUTH"'
✔ /workspace/apps/frontend1/.env.local
✔ /workspace/.env.dev
✔ /workspace/.env
✨ API_USERS http://localhost:3001/users
✨ API_AUTH https://dotenv-run.dev/api/v1/auth |
@dotenv-run/cli had nice feature needed for turbo repo: Supports hierarchical cascading configuration in monorepo projects (Nx, Turbo, etc.) apps/next-app/.env > apps/.env > .env but missing support for multiple custom .env files |
I think the whole way turborepo is handling environment variables along side the vercel cli is not well though through. So lets consider this, we have a monorepo with 2 applications
We open vercel Before I can run
to the Now this means that if we're testing I would expect maybe that doing
Instead of
|
turbo run dev —filter=apps/web other idea is to have common .env at project root level and per-subproject .env in apps/web level. |
Hi @xmlking thanks for your reply. So would you do the Following my structure in the example above
And then do
And that "links" you to the 2 applications so you can run |
Got an update, not sure if its helpful but please have a read here |
@chihab generously added turborepo support with some additional features I requested: https://dotenv.run/ {
"scripts": {
"dev": "dotenv-run -p .env,.secrets -- vite dev",
"build": "dotenv-run -p .env,.secrets -- vite build",
"preview": "dotenv-run -p .env,.secrets -- vite preview",
}
} in debug mode, it loads env files like this |
We've updated our environment variable guidance in new documentation: https://turbo.build/repo/docs/crafting-your-repository/using-environment-variables |
The new documentation is helpful but easy to miss when searching for Vercel build error messages that don't occur in local dev. For example:
The environment variable is set in the Vercel project and locally in the app/package, but it wasn't clear that 'environment variable is not set' referred to the root turbo.json and needed to be added under 'globalEnv' or 'env'. |
Which project is this feature idea for?
Turborepo
Describe the feature you'd like to request
I feel as though many users would greatly benefit from more helpful docs on "Using environment variables".
The only suggestion is to install
dotenv-cli
and add it infront of the script like"dev": "dotenv -- turbo dev"
.I'm currently working on adding
prisma
to to my monorepo for a shared db client. The turborepo docs on adding prisma are helpful but not in regards to environment variables like theDATABASE_URL
.Describe the solution you'd like
I feel like more suggestions for having a shared .env config in your monorepo would be ideal.
Regardless of how this is handled via a "package" in the monorepo or using a
.env
file from root without having to managedotenv-cli
especially when having multiple .env files like.env.local
.env.local.development
.env.local.production
This would be a great out of the box feature since people usually need some .env managment ESPECIALLY in a monorepo.
Describe alternatives you've considered
I feel as though my [Describe the solution you'd like] section hits this point already.
I've tried using the recommended
dotenv-cli
approach from the docs and it doesn't work for my use case.This is the approach I tried using my .env file from root with prisma and turbo:
In root package.json:
"db:migrate": "dotenv -e ./env -- turbo db:migrate"
In packages/database/package.json:
"db:migrate": "prisma migrate dev --name init"
Running
npm run db:migrate
from root gives me this error message:database:db:migrate: error: Environment variable not found: DATABASE_URL.
I understand that this could potentially be labeled as an issue with prisma. But, the question still stands for managing environment variables throughout your turborepo
The text was updated successfully, but these errors were encountered: