Let’s pick up where we left off:
workspaces
NPM Docs is defined as an array of file paths, and when installing dependencies, each workspace is searched and the dependencies of those workspace are connected to node_modules in the project root directory.
In the following example, as long as folder Packages contains a valid package.json file, all folders within that folder will be treated as a workspace:
{
"name": "workspace-example",
"workspaces": [
"./packages/*"
]
}
Copy the code
Defining the workspace
Workspaces typically define package.json through the workspaces property of a file, for example:
{
"name": "my-workspaces-powered-project",
"workspaces": [
"workspace-a"
]
}
Copy the code
This directory contains a folder called workspace-a, which itself contains a package.json inside that defines a Node.js package, for example:
+-- package.json '-- workspace -a' --package.jsonCopy the code
Expected results once NPM Install is running in the current working directory. Is the folder workspace -A that will be symlinked to node_modules’ current working directory.
Creating a workspace
We will add a workspace named packagesTest to the root directory. After executing the command, we will add a packagesTest folder (containing package.json) to the root directory.
npm init -w ./packagesTest
Copy the code
Add dependencies to your workspace
npm install jquery -w a
Copy the code
Add jquery dependencies to Workspace A
Run the command in the workspace context
You can use the Workspace configuration option to run commands in the context of a configured workspace
// Run the test command in workspace A
npm run test --workspace=a
// Run the test command in workspaces A and B
npm run test --workspace=a --workspace=b
// You can also use the 'workspaces' configuration option to enable the same behavior by executing the test command in all workspaces
npm run test --workspaces
Copy the code
extension
yarn workspace
Using workspaces in YARN is very similar to using workspaces in NPM, but requires a private field
{
"private": true,
"workspaces": ["workspace-a", "workspace-b"]
}
Copy the code
Note that private: true is required! Workspaces are not intended to be published, so this security measure is added to ensure that nothing will accidentally expose them. At this point, we install dependencies and it becomes YARN Install
Some common commands
Yarn workspaces info // Add dependencies // Yarn workspace <workspace_name> add <packageName> YARN workspace workspace-a add React react-dom --dev // remove dependencies // yarn workspace <workspace_name> remove <packageName> yarn workspace workspace-a remove react-dom --saveCopy the code
pnpm workspace
A file pnpm-workshop.yaml is required in the root directory
Pnpm-workspace.yaml defines the root directory of your workspace and enables you to include/exclude directories from your workspace. By default, all packages for all subdirectories are included. For example (test file as workspace)
packages:
- 'test/**'
Copy the code
Monorepo is a way of managing project code. It refers to managing multiple modules/packages in a project repository (REPO). The main benefit of Monorepo is the unified workflow and Code Sharing. Unified modification, unified testing, unified version. With just one set of scaffolding, you can manage (build, test, publish) multiple packages.
The most common Monorepo solution is the workspaces feature of Lerna and YARN. For those of you who are interested, check it out.