Yarn add creates yarn.lock and node_modules folder inside sub-package in yarn workspace

Viewed 676

I am using a yarn workspace, and wonder why yarn creates a yarn.lock and a node_modules folder in the sub-projects when adding dependencies via yarn add ....

As an example, imagine the following simple yarn workspace:

{
  "private": true,
  "workspaces: [
    "./packages/*"
  ]
}

File structure with sub-project named "subpackage".

enter image description here

Then I add a dependency to the "subpackage" using the following command:

yarn workspace subpackage add lodash

After that, the file folders change as follows:

enter image description here

I wonder why yarn creates the yarn.lock and the node_modules folder when I am in a workspace and even explicitly use the workspace version of the add command. Shouldn't all dependencies be managed through the root workspace directory?

I didn't really find a clear answer to this in the yarn documentation, however this blog-post, which introduced the workspace feature, clearly confirms the expected behaviour:

If you want to modify a dependency of a Workspace, just run the appropriate command inside the Workspace folder:

$ cd packages/jest-matcher-utils/
$ yarn add left-pad
✨ Done in 1.77s.
$ git status
modified: package.json
modified: ../../yarn.lock

Note that Workspaces don’t have their own yarn.lock files, and the root yarn.lock contains all the dependencies for all the Workspaces. When you want to change a dependency inside a Workspace, the root yarn.lock will be changed as well as the Workspace’s package.json.

0 Answers
Related