Setting Up a MERN Monorepo Using Yarn Workspaces


A monorepo (short for "monolithic repository") is a single repository that contains multiple projects, which can be related or unrelated. This setup is helpful for developers as it simplifies dependency management, improves development workflow, and supports better code sharing between projects.
In this guide, we'll walk through setting up a MERN (MongoDB, Express, React, Node.js) project inside a monorepo using Yarn Workspaces.
monorepo-mern/
├── client # React + Vite + TypeScript
├── server # Express + TypeScript
└── README.md
Setup with Yarn
Run yarn init
in the root of the project directory, and then install concurrently
package.
yarn init -y
yarn add concurrently -W
The
-W
flag installs the package at the workspace root level (i.e., "W" for "workspace-root").
Edit the root package.json
to include your client
and server
folders as workspaces:
"workspaces": [
"server",
"client"
],
then cd
into client
and install the package using yarn
similarly do for server
folder. Add to configure scripts for client
& server
.
# scripts for client
"scripts": {
"dev": "vite",
"build": "tsc -b && vite build",
"format": "prettier --write .",
"format:check": "prettier --check .",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"preview": "vite preview"
},
# scripts for server
"scripts": {
"build": "tsc -b",
"start": "tsc -b && node ./dist/index.js",
"dev": "nodemon --exec ts-node ./src/index.ts",
"clean": "rm -rf dist/ .logs/",
"format": "prettier . --write",
"format:check": "prettier . --check",
"lint": "eslint .",
"lint:fix": "eslint . --fix"
},
At the root package.json
, configure scripts to run both client
and server
simultaneously:
"scripts": {
"dev": "concurrently \"yarn workspace client dev\" \"yarn workspace server dev\"",
"build": "concurrently \"yarn workspace client build\" \"yarn workspace server build\"",
"format": "concurrently \"yarn workspace client format\" \"yarn workspace server format\"",
"lint": "concurrently \"yarn workspace client lint\" \"yarn workspace server lint\"",
"clean": "concurrently \"yarn workspace client clean\" \"yarn workspace server clean\""
}
🧠
concurrently
lets you run multiple scripts in parallel, which is perfect for monorepos.
Troubleshooting Tips
Make sure you run
yarn
at the root after every major change.If you face any issues related to package mismatches or missing dependencies:
Run
yarn install
again at the root.Clear the cache using
yarn cache clean
.Double-check your workspace paths in
package.json
.
Summary
Using a monorepo with Yarn Workspaces in a MERN stack app offers:
Centralised dependency management
Easier simultaneous development
Cleaner and modular project structure
This structure is especially useful in team-based or full-stack projects, helping streamline both frontend and backend workflows.
Final Note
This setup is just a foundation. You can further improve it by:
Adding a
.env
management tool likedotenv
Adding shared packages or types
Using tools like Turborepo or Nx for advanced workflows
Subscribe to my newsletter
Read articles from Abhinab Choudhury directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
