I ve setup a basic turborepo project and I want to share .env
variables across the all the apps and some of packages. If I set one .env
file in the root of project and how can all apps and packages access them. Or for my requirement do I need to set multiple .env
files in all apps and packages?
As of December'22, the recommended way of doing this as per the official turbo
docs is as follows:
cd root-directory-of-your-project
.npm add -D dotenv-cli
(or with pnpm
: pnpm add -D dotenv-cli -w
)..env
file in the root directory of your project.
.env
to your project's root .gitignore
..env
file.
nextjs
remember to prefix your public variables with NEXT_PUBLIC_*
(example: NEXT_PUBLIC_GOOGLE_ANALYTICS_TOKEN=1234
).turbo.json
file, add the variables on which each pipeline
job depends.
{
"$schema": "https://turborepo.org/schema.json",
"pipeline": {
"dev:frontend": {
"outputs": ["dist/**", ".next/**"],
"env": ["NEXT_PUBLIC_GOOGLE_ANALYTICS_TOKEN"]
},
"dev:backend": {
"outputs": ["dist/**", ".next/**"],
"env": ["DATABASE_URL"]
}
}
}
process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS_TOKEN
.Once finished, the structure of the project should look similar to the following:
apps
backend <<< Your backend code. You don't need to keep any .env file here.
frontend <<< Your frontend code. You don't need to keep any .env file here.
package.json <<< Where you should install dotenv-cli as a dev dependency.
turbo.json <<< Where pipeline jobs and their dependencies on environment variables are specified.
.env <<< Where all your environment variables will be stored.
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