I have a monorepo with the following script:
"start": "env-cmd -f .env turbo run start --parallel",
If I run yarn start at the root, it runs all my apps using the .env file at the root.
A lot of my environment variables are only used in one specific app and my /.env file is getting cluttered as I keep on adding new apps. I want to keep that .env file for shared environment variables only and have children .env files at the root of each apps for specific configurations.
Example if I have an app called web located in /apps/web, I would like to add an /apps/web/.env file that is used only when building the web app.
How can I achieve that ?
.env file. They will be available in all your workspaces by default.dotenv-cli package as dev_deps for workspaces that require .env file with workspace-specific variables.package.json:"with-env": "dotenv -e ./.env --""dev": "pnpm with-env next dev","build": "pnpm with-env next build"globalEnvs and add .env files (with workspace-specific envs) to your workspaces to the root (package.json level).Not sure how to run both root and app level, but if you want just the app level, do the following:
turbo.json put in all the keys:"globalEnv": [
"MY_KEY",
"MY_OTHER_KEY"
],
//a. ".env" is the default, you don't need the following line unless you have a custom .env file name. e,.g. .env.local
//b. Note "globalDependencies" in the following line is referring to your .env file under the app folder, not the root one.
"globalDependencies": [".env.local"]
.env or .env.local file there.MY_KEY='SOMETHING'
MY_OTHER_KEY='SOMETHING ELSE'
turbo run dev --filter=my-web-project
That's it, your web app will pick up the env file in your app folder, not from your root folder.
ref: https://turbo.build/repo/docs/reference/configuration#globalEnv
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With