t3-env in a monorepo
When using t3-env in a monorepo where different sub-packages need different parts of the env, is it recommended to create a sub-package specifically for the env and put all of the env vars in there, or for each sub-package to manage its own env?
The issue with the latter is of course that sometimes multiple packages depend on the same env vars, leading to code duplication. The issue with the former approach is that it feels like poor encapsulation and gives packages access to env vars that don't apply to them.
Solution:Jump to solution
I would make one env package and then have seperate files that are imported where you need them, here’s how I do it:
https://github.com/AnswerOverflow/AnswerOverflow/tree/main/packages/env...
GitHub
AnswerOverflow/packages/env at main · AnswerOverflow/AnswerOverflow
Indexing Discord Help Channel Questions into Google - AnswerOverflow/AnswerOverflow
5 Replies
Solution
I would make one env package and then have seperate files that are imported where you need them, here’s how I do it:
https://github.com/AnswerOverflow/AnswerOverflow/tree/main/packages/env
GitHub
AnswerOverflow/packages/env at main · AnswerOverflow/AnswerOverflow
Indexing Discord Help Channel Questions into Google - AnswerOverflow/AnswerOverflow
Nice, thanks for sharing!
Quick question, can I ask why in your env package you have shared.ts as well as shared-schema.mjs, which seems to be very similar? Same with web.ts and web-schema.mjs
One is for my tsup.ts file and the other if for my next config .mjs file
It’s dumb but I just kind of gave up on fighting with JS tooling and duplicated them
Once next config supports typescript I’ll get rid of the .mjs ones
Hey @Rhys as reece, I just opened a PR on the t3-env repo that adds support for extending envs. Since you're doing something similar in AnswerOverflow, I was wondering if you could offer any feedback and let me know if you like this approach? https://github.com/t3-oss/t3-env/pull/158
GitHub
feat: support extending envs by b0o · Pull Request #158 · t3-oss/t3...
In some case, different parts of an application may depend on different environment variables. This is especially common in monorepos.
As it stands, it's difficult to use t3-env in cases where ...