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:
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
Jump to solution
5 Replies
Solution
Rhys
Rhys9mo ago
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
b0o
b0o9mo ago
Nice, thanks for sharing!
b0o
b0o9mo ago
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
Rhys
Rhys9mo ago
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
b0o
b0o9mo ago
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 ...
Want results from more Discord servers?
Add your server