I'm working on a monorepo setup using pnpm
workspaces to manage multiple related projects. My goal is to efficiently manage and share dependencies across these projects without duplicating them in each project's package.json
. However, I'm facing challenges with resolving shared dependencies and ensuring that binaries are correctly executed.
package.json
. This helps avoid redundancy and ensures consistency.rimraf
) that need to be available when running scripts in individual projects. These binaries need to be correctly resolved, even if they are defined in a shared or root configuration.package.json
or the root package.json
with unnecessary dependencies.pnpm-workspaces.yaml
packages:
- "packages/*"
- "packages-foo/*"
- "packages-bar/*"
Root package.json
(workspace root):
{
"name": "monorepo-root",
"devDependencies": {
"eslint": "^7.32.0",
"prettier": "^2.3.2",
"rimraf": "^3.0.2"
}
}
Shared Configurations package.json
(packages/shared/package.json):
{
"name": "shared-config",
"devDependencies": {
"@faker-js/faker": "^7.4.0",
"typescript": "^5.5.3"
}
}
API Package package.json
(packages/api/package.json):
{
"name": "api-package",
"scripts": {
"build": "pnpm exec rimraf dist && pnpm exec tsc",
"lint": "pnpm exec eslint src --fix"
},
"devDependencies": {
"typescript": "workspace:*"
}
}
Web Package package.json
(packages/web/package.json):
{
"name": "web-package",
"version": "1.0.0",
"private": true,
"scripts": {
"build": "pnpm exec rimraf dist && pnpm exec tsc",
"lint": "pnpm exec eslint src --fix"
},
"devDependencies": {
"typescript": "workspace:*"
}
}
rimraf
are not found unless they are explicitly listed in each package's devDependencies
.@faker-js/faker
in the shared
package are not being resolved by other packages, such as api-package
, even though they are intended to be shared.pnpm
workspaces to handle this efficiently?Is this even possible with pnpm
workspaces, or am I trying to achieve something that isn't feasible?
Any guidance or examples of how to set this up effectively would be greatly appreciated!
That is not possible. You cannot share dependencies like this. You cannot access dependencies of dependencies. If these are dev dependencies, just add them to the root package.json. Otherwise, you need to duplicate the dependencies wherever you use them. You can use catalogs to sync the versions of such dependencies across the workspace.
There was a rejected RFC to support something similar. Core npm/node.js contributors said it was a bad idea.
If you want a way to share dependencies, you could also check Bit workspace. Bit has environment components, which allow to share dependencies the way you want.