w3resource

Yarn why and yarn workspaces CLI commands


There are lots of packages installed during development. If you are working in a group, you might not know why a dependency was installed. This tutorial introduces the yarn why CLI command, it will also show you how to display information about your workspaces.

Yarn why

As stated in the introduction, the yarn why command will display information about why a package is installed.

The syntax for running this command is as shown below:

yarn why <query>

When this command is run, it will identify why a package was installed; it will also give you details on the packages that depend on it.

For example, it will state whether the package was explicitly marked as a dependency in the package.json manifest.

yarn why

Sample yarn why <query> command

Query argument

The yarn why requires some mandatory query arguments, which can be any of these:

  • The package name (as in the example above)
  • The package folder; eg: yarn why node_modules/react-dom
  • a file within a package folder; eg: yarn why node_modules/once/react-dom

The file locations can equally be absolute.

Yarn workspaces

As from Yarn v1.0, workspaces are enabled by default; so you don't have to do anything to configure workspaces. If you find that workspace is not set, you can run the following command:

yarn config set workspaces-experimental true

The yarn workspaces command shows information about your workspaces.

Commands

yarn workspaces info [--json]

When you run the command above, it will display the workspace dependency tree of your current project.

yarn workspace info

yarn workspaces vx.x.x

{ "create-subscription": {
    "location": "packages/create-subscription",
    "workspaceDependencies": [],
    "mismatchedWorkspaceDependencies": []
  },
  ...
  "react-noop-renderer": {
    "location": "packages/react-noop-renderer",
    "workspaceDependencies": [
      "react-reconciler"
    ],
    "mismatchedWorkspaceDependencies": []
  },
  "react-reconciler": {
    "location": "packages/react-reconciler",
    "workspaceDependencies": [],
    "mismatchedWorkspaceDependencies": []
  }, ... }

yarn workspace run <command>

When you run this command, it will run the chosen yarn command in each workspace.

For instance, running yarn workspaces run test will invoke the test script for each workspace. It will also pass forward flags and you can use it for CI processes as shown below:

yarn workspaces run test -ci

Previous: Yarn version and yarn versions CLI commands
Next: Yarn configuration and the package.json file