Skip to main content

npm Workspaces

From SCRUM-12, SCRUM-13: Set up monorepo structure

What are npm Workspaces?

npm workspaces is a built-in feature of npm that lets you manage multiple packages in one repository.

Setting Up

1. Root package.json

{
"name": "vocabpal-learning",
"private": true,
"workspaces": [
"shared",
"extension",
"web"
]
}

2. Workspace package.json

// shared/package.json
{
"name": "@vocabpal/shared",
"version": "1.0.0",
"main": "dist/index.js",
"types": "dist/index.d.ts"
}

Common Commands

Install all dependencies

npm install  # Run from root

Run command in specific workspace

npm run build -w shared
npm run test -w extension

Run command in all workspaces

npm run build --workspaces

Add dependency to workspace

npm install lodash -w shared

Add dev dependency to root

npm install -D typescript

Using Workspace Packages

Reference workspace packages by name:

// extension/package.json
{
"dependencies": {
"@vocabpal/shared": "*"
}
}

Then import in code:

import { Word, wordService } from '@vocabpal/shared';

VocabPal Structure

VocabPal-Learning/
├── package.json # Root - defines workspaces
├── shared/
│ └── package.json # @vocabpal/shared
├── extension/
│ └── package.json # @vocabpal/extension
└── web/
└── package.json # @vocabpal/web

TODO: Add notes from Jira tickets SCRUM-12, SCRUM-13