Is it possible to share config variables / env variables across subfolders of a monorepo that is set up using yarn workspaces? We've got a monorepo for a project, and several of the subfolders are projects that are built using create-react-app. In those individual folders, we can have .env files to specify config values, and they get used fine when we use the build/start scripts in our package.jsons at the individual level.
However, we've also got other subfolders that are just libraries that are imported into the CRA apps. We'd like to specify config/env variables in those libraries, but so far haven't found a way to get the values to propagate when we build or start a project that imports the library. Have tried .env files in the libraries themselves as well as in the CRA app root folders, but nothing seems to work...
Consider the implications of reading from .env
as this may adverse affect third-party libraries and dependencies into process.env
.
You can use libraries like https://github.com/motdotla/dotenv to do that:
.env.file
file in your lib:- src
- index.js
- .env.file
import dotenv from 'dotenv'
import path from 'path'
dotenv.config({
path: path.join(__dirname,'..','.env.file'),
})
// the rest of the file...
You can use find-yarn-workspace-root
to find the root directory of your repository.
import workspacesRoot from "find-yarn-workspace-root";
import { config as dotenv } from "dotenv";
const rootDirectory = workspacesRoot();
dotenv({ path: `${rootDirectory}/.env` });
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