[Monoepo] Yarn Workspaces command

2024-04-13 hit count image

Let's see how to execute the commands set in the scripts of each project in a monorepo configured using Yarn's Workspaces.

Outline

In this blog post, I will introduce how to execute the commands set in the scripts of each project when configuring a monorepo using Yarn’s Workspaces.

Blog Series

This blog is a series. Please check other blog posts through the following links.

Example

In order to learn how to execute the commands set in the scripts of each project in a monorepo configured using Yarn’s Workspaces, let’s create an example. First, create the following folder and file structure.

.
├── package.json
└── src
    ├── module-a
    │   ├── index.js
    │   └── package.json
    └── module-b
        ├── index.js
        └── package.json

The package.json of module-a is as follows.

// src/module-a/package.json
{
  "name": "module-a",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  }
}

The package.json of module-b is as follows.

// src/module-b/package.json
{
  "name": "module-b",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  }
}

And the index.js of module-b is as follows.

// src/module-b/index.js
console.log('module-b');

The index.js of module-a is as follows.

// src/module-a/index.js
console.log('module-a');

require('module-b');

Lastly, modify the package.json file in the root folder as follows.

{
  "name": "monorepo",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "private": true,
  "workspaces": {
    "packages": ["src/*"]
  }
}

Now, run the following command to install the packages and execute the commands set in the scripts of module-a and module-b.

yarn install
cd ./src/module-a
yarn start

cd ../..

cd ./src/module-b
yarn start

Then, you can see that module-a and module-b are executed well as follows.

# cd ./src/module-a
# yarn start
module-a
module-b
# cd ./src/module-b
# yarn start
module-b

Workspaces command

In the example above, you can execute the commands set in each project like following.

yarn install
cd ./src/module-a
yarn start

cd ../..

cd ./src/module-b
yarn start

However, it is not efficient to go into the project and execute the command every time.

To solve this problem, Yarn supports the following Workspaces command.

yarn workspace <workspace_name> <command>

By using this command, you can execute the command without moving to the project folder. <workspace_name> is the value set in the name of package.json, and <command> is the command set in the scripts. You can execute the command in the example above as follows.

yarn workspace module-a start
yarn workspace module-b start

When you run the above command, you can see that the result is displayed well as follows.

# yarn workspace module-a start
module-a
module-b
# yarn workspace module-b start
module-b

Completed

Done! We’ve seen how to execute the commands set in the scripts of each project in a monorepo configured using Yarn’s Workspaces. If you’ve been moving to the project and executing the command every time, try using the Workspaces command to execute the command more conveniently.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts