I have a monorepo with a structure that is close to:
packages/
client/
src/
package.json
server/
src/
package.json
shared/
src/
package.json
package.json
I want to install a package in the root folder, and use it in other packages.
To do this, I ran npm i zod in the root folder, then added zod as a peerDependency for all nested packages.
When I run npm install, I can see multiple instances of zod being installed across the monorepo. It exists on the root node_modules, but it also exists in other packages' node_modules.
I wouldn't bother this if zod didn't rely on having a single instance of it being installed. (You have to check for instanceof, and it won't work if the object was created in another package)
What's the best way of making sure I always use the same instance of an installed dependency?
I was thinking about using Lerna's --hoist option, or useWorkspaces: true, but both removed the package-lock.json from individual packages, and copied their contents with wrong dependency versions into the root package-lock.json. Seems a bit odd.