peach789456
peach789456

Reputation: 105

Why does pnpm v7 create multiple instances for the same library?

I am using pnpm workspace to manage my projects. The folder structure looks like this.

└── my-project/
    ├── apps/
    │   └── myapp/
    │       ├── package1
    │       ├── package2
    │       └── [email protected]
    ├── packages/
    │   ├── package1/
    │   │   └── [email protected]
    │   └── package2/
    │       ├── package1
    │       └── [email protected]
    ├── package.json
    └── pnpm-workspace.yaml
  1. myapp depends on package1/package2/styled-components.
  2. package1 depends on styled-components.
  3. package2 depends on package1/styled-components.

And all of them are included in package.json dependencies field.

When i checked /node_modules/.pnpm/ folder, i saw there are 2 folders generated for styled-components. I thought it should only have one instance but apparently it doesnot. Does someone know why pnpm behaves like this? Thank you.

PS: My env is: node: 18.15.0 pnpm: 7.29.2 .npmrc:

public-hoist-pattern[]=@types*
dedupe-peer-dependents=true
esolve-peers-from-workspace-root=true 

enter image description here

Upvotes: 0

Views: 471

Answers (1)

Zoltan Kochan
Zoltan Kochan

Reputation: 7666

It can happen with package that depend on peer dependencies. If style-components is in the dependencies of several projects or dependencies of the project, then it may happen that in one place it is resolved with one set of peer dependencies while in another place it is resolved with a different set of peer dependencies.

Usually setting auto-install-peers=true (which is true by default in pnpm v8) and dedupe-peer-dependents=true reduce the number of such duplicates.

You can also true to force the same version of the peer dependency (if it isn't resolved from a direct dependency) using overrides.

Also, I see a typo in your settings. The last setting is missing "r" from the beginning.

Upvotes: 0

Related Questions