简体   繁体   中英

How to use both root and app level env files with turborepo

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?

Not sure how to run both root and app level, but if you want just the app level, do the following:

  1. in your root 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"] 
  1. in your web project, put your .env or .env.local file there.
MY_KEY='SOMETHING'
MY_OTHER_KEY='SOMETHING ELSE'
  1. Clear all your cache in the cache folder (just in case), then run:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM