7

I use npm workspaces. When I install a package for a workspace using nmp i somepackage -w workspace-a it is placed in to the same directory with the workspace if the installed version is different from root version.

I want to move it to a sub dir of root node_modules dir. Is it possible?

current behaviour:

root
|--node_modules
|  |--somepackage
|--workspace-a
|  |--node_modules
|  |  |--somepackage
|  |--package.json
|--package.json

what I want is something like:

root
|--node_modules
|  |--somepackage
|  |--workspace-a
|  |  |--node_modules
|  |  |  |--somepackage
|--workspace-a
|  |--package.json
|--package.json
Stan Mayan
  • 271
  • 4
  • 11

1 Answers1

8

if the installed version is different from root version

is the key here and it's probably not possible as I can see why npm behaves correctly and installs 'duplicate' version in root/node_modules.

in my case I had React 18 along other deps correctly defined in workspace-a/package.json (by installing via npm install react -w worskspace-a). yet React 18 was installed in local worspace-a/node_modules while and React 17 in root/node_modules which was causing issues in the app runtime. there should be only one instance: React 18 in root/node_modules.

this was caused by one other sub-sub dependencies in workspace-a depending on react <=17 so npm install would install v17 additionally in root/node_modules. hence my correct behaviour note earlier.

I was able to get rid of the issue by defining peer dependency React 18 on the root level but it's all at risk of using peer deps conflicts and overrides (as npm would warn you). in my case the ultimate fix will be redefining deps in the other library (and their package.json) I am using in workspace a.

dmudro
  • 2,894
  • 1
  • 21
  • 23
  • You are right, this ended up being the problem in my case. I had some dependencies requiring React 18 instead of 17, that caused npm to create a `node_modules` folder inside one of the workspaces. – Camilo Aug 17 '22 at 19:20
  • Have you tried `npm install --legacy-bundling`? – Filip Seman Dec 14 '22 at 11:40