cotrav_monorepo_package_creation_service_build_guide
Table of Contents
Cotrav Monorepo – Package Creation & Service Build Guide
This document describes how to create shared packages and build/run services in the Cotrav monorepo. This is a centralizes multiple distinct projects, libraries, and apps into a single, unified repository, enabling easier code sharing, dependency management, and consistent tooling/standards across teams.
Repository Structure
cotrav-services/ │ ├─ package.json (root – workspaces enabled) │ ├─ packages/ (shared libraries) │ ├─ logger/ │ ├─ errors/ │ ├─ middlewares/ │ └─ tsconfig.json (base TS config for packages) │ ├─ workspace/ (runtime services) │ ├─ auth-service/ │ ├─ booking-service/ │ └─ payment-service/ │ └─ node_modules/
Prerequisites
| Tool | Version |
|---|
| Node.js | >= 18.x |
| npm | >= 9.x |
| TypeScript | >= 5.x |
| VS Code | Recommended |
Root Workspace Setup
Root package.json
The root enables npm workspaces so shared packages can be consumed without publishing.
cmd
npm install
Install Dependencies
Logger
npm install pino npm install -D @types/node
Error
npm install -D @types/node
Middleware
npm install express npm install -D @types/express @types/node
Build Package
package/logger
cmd
npm run build --workspace=packages/logger
package/errors
cmd
npm run build --workspace=packages/errors
Creating Error & Middleware Packages
Repeat the same steps for:
@cotrav/errors@cotrav/middlewares
Only business logic differs, structure remains identical.
Creating a Service
Example: auth-service
Folder Creation
cd workspace mkdir auth-service cd auth-service npm init -y
Service package.json
{
"name": "auth-service",
"version": "1.0.0",
"private": true,
"type": "commonjs",
"scripts": {
"dev": "ts-node src/app.ts",
"build": "tsc",
"start": "node dist/app.js"
},
"dependencies": {
"express": "^5.0.0",
"dotenv": "^17.0.0",
"@cotrav/logger": "*",
"@cotrav/errors": "*",
"@cotrav/middlewares": "*"
}
}
Install Dependencies
npm install npm install -D typescript npm install express dotenv jsonwebtoken bcryptjs swagger-ui-express swagger-jsdoc pino
Type deps
npm install -D @types/express @types/node @types/jsonwebtoken @types/bcryptjs @types/swagger-ui-express
Build Services & Packages
Execute below command on root folder “cotrav-services”
npm run build npm run typecheck
Run auth service in dev
npm run dev:auth
Clean all builds
npm run clean
Sanity check (must work)
npm workspaces list
cotrav_monorepo_package_creation_service_build_guide.txt · Last modified: by pradnya
