'share env variable beween turborepo monorepo project

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?



Solution 1:[1]

You can specify the globalDependencies option in your turbo config or use the --global-deps CLI flag.

{ 
   // ... rest of the turbo config
   globalDependencies: ['.env']
}

or

turbo run build --global-deps=".env.*"

References from docs:

https://turborepo.org/docs/reference/configuration#globaldependencies https://turborepo.org/docs/reference/command-line-reference#--global-deps

Solution 2:[2]

To load the env variables from .env into the process env you can use https://www.npmjs.com/package/dotenv.

Then to share the env variables in your monorepo :

In each workspace/app add require('dotenv').config({path: /custom/path/to/.env}) (assuming common js module resolution here) as early as possible as per docs (meaning e.g. for a next js app in next.config.js), where /custom/path/to/.env would be the relative path to your root .env (e.g. two folders up:../../.env)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2