r/webdev • u/KnowBearFeet • 29d ago
Discussion How do you organize your full-stack mono-repo?
Just as the title asks. I am curious how you organize the various parts. I have been on teams that both do and don't use mono-repos. For the ones that did, there has been a wide variety of choices across the app and how it's organized.
Just as a small example: naming conventions. Sometimes the the repo is named for the app (let's say "OurApp") and the various parts of the file structure are "OurApp-", like OurApp-ui and OurApp-api and OurApp-db. Or there might just be a top-level folder for "frontend", "backend" and "database", etc.
I'm not talking about a debate of mono-repo vs. multi-repo approaches, but in a mono-repo, what are some of the code organization techniques you've liked and disliked in your experience?
UPDATE: The community turned out! Thanks for sharing your thoughts in the comments. I appreciate the discussions.
6
u/Alarming_Ad_257 28d ago
I use Nx with
apps/(web, admin, api, worker) andlibs/split by domain (orders, finance, etc.) plus a shared db lib for Prisma.Biggest wins: organizing by domain instead of by layer, and sharing types between frontend and backend through tRPC, so backend changes break the build, not prod.
Biggest mistake I’ve seen: a giant
utils/orcommon/folder everything depends on. It turns into a junk drawer fast.Also, skip the
OurApp-apinaming. The repo name already tells you which app it is.11
u/_listless 29d ago
usually something like:
5
u/ItsAllInYourHead 29d ago
I struggle with this myself. I generally try to keep the root as empty/uncluttered as possible. In my latest project, my root has:
.gitignoreCLAUDE.mdcompose.yml- spins up the entire project locally with docker composelefthook.yml- global git hooks for the project,. subfolders can have their own, too.mise.toml- I use mise, and this is the root config, subfolders have their own config.README.mdand it has the following subfolders:
admin-web- the web interface for administration (i keep this separate from the main siteapi-server- the backend API serverauth-server- the backend auth servermailer-server- the backend service that handles sending transactional emailsmobile-app- The (Expo/React Native) mobile appweb- The main web app/web page.I still don't love it. I never do. But I like it enough. My next project will probably have some tweaks. I go back and forth between using a prefix vs suffix to specify the "type" (i.e. api-server vs server-api).
Two things to note:
2
u/Potential-Still 29d ago
I use NX.
37
u/Discordbothost 29d ago
the rule that's worked best for me: if it gets deployed on its own it goes in apps/, if it only gets imported by other things it goes in packages/. so apps/web, apps/api, packages/db, packages/ui, packages/config. you stop arguing about client/server vs ui/api because the folder tells you what it is by how it ships, and the names don't need a repo prefix since the repo is already the app.
only gotcha is shared types. put them in their own tiny package early, otherwise the frontend ends up importing from the api folder and you get circular headaches later.
4
u/barrel_of_noodles 29d ago
I use a framework? ... So I don't have to make these decisions?
Its like if I started building a car by planting some rubber trees and buying mining equipment for ore.
3
u/devenition 29d ago
Keep the root as empty as possible but not any emptier. Too often the main app takes the root and other things take subfolders, or you have 5 different prettier configs all nearly identical
1
u/m_redditUser 28d ago
3
u/Tarazena 29d ago
I like pnpm workspaces approach, apps folder contain apps, packages contain packages, and root orchestrate those apps/packages
3
u/Squigglificated 28d ago
Be careful about generic packages like "types" or "utils". They quickly become dumping grounds for completely unrelated things. And you quickly end up with every package depending on every other package. And any change in any package causes everything else to be rebuilt.
Once that happens you basically have a monolith with extra steps.
Limit the scope of what a package does and needs.
Our monorepo has a generic ui package. It has many components, but it only contains pure display components that doesn't depend on any feature specific apis or types.
Feature specific functionality goes directly in the apps or in feature packages instead.
If one specific component or utility drags in a lot of extra dependecies that nothing else in the package needs it might be better as a separate package.
Organizing vertically by feature can also work. That lets you see everything related to a feature in one place.
Enforce the architecture boundaries you decide on with lint rules or something like Fallow.
With file naming decide on something consistent and stick with it. kebab, camel or pascal case doesn't really matter, just decide something.
apps/ and packages/ is a good start.
Agree with the person who said not to abstract too early. Needing something in exactly two places doesn't justify an abstraction. Something duplicated 20 times usually does.
1
u/nSpace1988 28d ago edited 28d ago
Have everything in /packages (we work mainly on libs)
There's a package for a shared eslint config that is consumed internally and published for consumers outside the monorepo. Prettier and typescript have root level configs that are extended per package needs.
1
u/Even_Gap_2253 29d ago
Depends on if it is company-wide monorepo or project-wide monorepo.
But generally speaking nothing different from multiple repos, you just change repo into directories.
2
u/onesneakymofo 28d ago
I use Rails. It does it for me.
2
u/Spare_Message_3607 28d ago edited 28d ago
pkgs/
- core/
- auth/
- ui/
apps/
- dash/
- public/
- api/
docs/
- PRODUCT.md
- ROADMAP.md
- KNOWN-ISSUES.md
- …
pnpm-workspace.yaml
package.json
vite.config.ts
AGENTS.md
2
u/gfranxman 28d ago
Folders for: frontend, backend, k8s, terraform, docs
2
u/ledatherockband_ 27d ago
My data pipeline is a modular, event-driven system. each project in the mono repo just imports the module it needs and lays them out in the sequence it needs.
my web app is a hexagonally architected monolith.
3
u/ScaredFlamingo6807 29d ago
I have nothing meaningful to contribute but I am commenting to remind myself to check this thread later. I am also interested in organization approaches!
1
u/Common_Suspect_2927 29d ago
I've seen teams put a top level `apps/` and `packages/` split so the deployable stuff stays separate from the shared code and thats the one layout I keep coming back to because it makes it obvious what ships vs what's ju
1
u/tech_cynic_dan 28d ago
How are people handling database migrations in their monorepos? Keeping them with the API or in a separate package?
-1
u/rwilcox 29d ago
packages/frontend
packages/backend
packages/shared
packages/related-tool-ie-llm-supporting-things
Starting off with frontend and backend folders, then eventually you’ll want shared…
1
u/[deleted] 28d ago
[removed] — view removed comment