NPM Workspaces - Some modules installed to local node_modules dir
Asked Answered
I

1

13

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
Incognizant answered 9/2, 2022 at 11:23 Comment(3)
Having a similar problem, something is causing npm install to created a node_modules folder at the workspace level, did you manage to solve it?Phonologist
No. I just accepted it as it is :)Incognizant
I managed to solve it by resolving dependencies version conflicts, as suggested by @dmudro answer.Phonologist
J
12

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.

Jochebed answered 3/4, 2022 at 6:37 Comment(2)
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.Phonologist
Have you tried npm install --legacy-bundling?Actinoid

© 2022 - 2024 — McMap. All rights reserved.