Skip to content

Add Workspace

Workspaces are folders that contain packages or applications. In a monorepo, workspaces are used to group related packages and applications together. In this guide, you will learn how to add new workspaces to your monorepo.

Command

This command allows you to add new workspaces to the monorepo.

bash
monopkg workspace add [workspaces...] [options]
bash
bun x monopkg workspace add [workspaces...] [options]
bash
npx monopkg workspace add [workspaces...] [options]
bash
yarn dlx monopkg workspace add [workspaces...] [options]

FYI

In the interactive mode, you can enter multiple workspace name to be added. The prompt will keep asking for the workspace name until you press Enter without entering a name.

Options

  • -s, --scope - Set the scope name of the new workspace.
  • --cold - Cold run, skip creating the workspace folder.

Examples

Single Workspace

Add a new workspace named tools:

bash
monopkg workspace add tools
bash
bun x monopkg workspace add tools
bash
npx monopkg workspace add tools
bash
yarn dlx monopkg workspace add tools

Output

Folder Structure

json
├─ packages
|  ├─ pkg-a
|  ├─ pkg-b
├─ tools 
|  └─ .gitkeep 
└─ package.json

package.json

json
{
  "workspaces": [
    "packages/*",
    "tools/*"
  ]
}

With Custom Scope

Add a new workspace named tools with a custom scope @app-tools:

bash
monopkg workspace add tools --scope @app-tools
bash
bun x monopkg workspace add tools --scope @app-tools
bash
npx monopkg workspace add tools --scope @app-tools
bash
yarn dlx monopkg workspace add tools --scope @app-tools

Output

Folder Structure

json
├─ packages
|  ├─ pkg-a
|  ├─ pkg-b
├─ tools 
|  └─ workspace.json 
└─ package.json

package.json

json
{
  "workspaces": [
    "packages/*",
    "tools/*"
  ]
}

tools/workspace.json

json
{
  "name": "tools", 
  "scope": "@app-tools"
}

Custom Scope

By setting a custom scope, the workspace will have its own workspace.json file with the specified scope. When creating a new package in this workspace, the scope will be used as the default scope for the package in the interactive mode.

Multiple Workspaces

Add multiple workspaces named tools and apps:

bash
monopkg workspace add tools apps
bash
bun x monopkg workspace add tools apps
bash
npx monopkg workspace add tools apps
bash
yarn dlx monopkg workspace add tools apps

Output

Folder Structure

json
├─ apps 
|  └─ .gitkeep 
├─ packages
|  ├─ pkg-a
|  ├─ pkg-b
├─ tools 
|  └─ .gitkeep 
└─ package.json

package.json

json
{
  "workspaces": [
    "apps/*", 
    "packages/*",
    "tools/*"
  ]
}

Custom Scope

When adding multiple workspaces, custom scopes can only be set in the interactive mode. Use the monopkg workspace add without any arguments to enter the interactive mode.


Skip Creating Workspace Folder

Add a new workspace named tools and skip creating the workspace folder:

bash
monopkg workspace add tools --cold
bash
bun x monopkg workspace add tools --cold
bash
npx monopkg workspace add tools --cold
bash
yarn dlx monopkg workspace add tools --cold

Output

Folder Structure

json
├─ packages
|  ├─ pkg-a
|  ├─ pkg-b
└─ package.json

package.json

json
{
  "workspaces": [
    "packages/*",
    "tools/*"
  ]
}

Custom Scope

When using the --cold option, the workspace folder will not be created, and the workspace.json file will not be generated. Do not use the --cold option if you want to set a custom scope, as the workspace.json file is required for that.

Released under the MIT License.