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.
monopkg workspace add [workspaces...] [options]
bun x monopkg workspace add [workspaces...] [options]
npx monopkg workspace add [workspaces...] [options]
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
:
monopkg workspace add tools
bun x monopkg workspace add tools
npx monopkg workspace add tools
yarn dlx monopkg workspace add tools
Output
Folder Structure
├─ packages
| ├─ pkg-a
| ├─ pkg-b
├─ tools
| └─ .gitkeep
└─ package.json
package.json
{
"workspaces": [
"packages/*",
"tools/*"
]
}
With Custom Scope
Add a new workspace named tools
with a custom scope @app-tools
:
monopkg workspace add tools --scope @app-tools
bun x monopkg workspace add tools --scope @app-tools
npx monopkg workspace add tools --scope @app-tools
yarn dlx monopkg workspace add tools --scope @app-tools
Output
Folder Structure
├─ packages
| ├─ pkg-a
| ├─ pkg-b
├─ tools
| └─ workspace.json
└─ package.json
package.json
{
"workspaces": [
"packages/*",
"tools/*"
]
}
tools/workspace.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
:
monopkg workspace add tools apps
bun x monopkg workspace add tools apps
npx monopkg workspace add tools apps
yarn dlx monopkg workspace add tools apps
Output
Folder Structure
├─ apps
| └─ .gitkeep
├─ packages
| ├─ pkg-a
| ├─ pkg-b
├─ tools
| └─ .gitkeep
└─ package.json
package.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:
monopkg workspace add tools --cold
bun x monopkg workspace add tools --cold
npx monopkg workspace add tools --cold
yarn dlx monopkg workspace add tools --cold
Output
Folder Structure
├─ packages
| ├─ pkg-a
| ├─ pkg-b
└─ package.json
package.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.