Skip to content

--install-strategy=nested still hoists workspace packages #6537

Open

Description

@pcone

Is there an existing issue for this?

  • I have searched the existing issues

This issue exists in the latest npm version

  • I am using the latest npm

Current Behavior

Currently, running npm install at the root of a monorepo using npm workspaces with the '--install-strategy=nested' flag almost does what I would expect it to do - it puts all the dependencies in separate node_modules folders for each package, but it still puts the symlinks to the local monorepo packages at the root.

Expected Behavior

I would expect the symlinks to the monorepo packages to be inserted into the nested node_modules folders rather than the root (and only for the packages that depend on them).

I also noticed that the --omit=dev flag seems to not work in this use case - note in the linked repro repo running 'npm install --install-strategy=nested --omit=dev' still results in eslint being added to workspace2's node_modules folder, even though it's only a devDependency. (I can make a separate issue for this if that would be better?)

Note for context: My use case here is a monorepo with multiple node apps in it, each of which needs to be separately bundled into a docker image with only it's dependencies, I don't want to unnecessarily include the dependencies of other packages in the monorepo. The '--install-strategy=nested' flag seems like what I need for my use case, but the current behaviour doesn't quite work.

Steps To Reproduce

  • make sure you have npm 9.7.1
  • clone this repo: https://github.com/pcone/npmissuerepro
  • run 'npm install --install-strategy=nested --omit=dev' at the root
  • note that a node_modules folder is still created at the root
  • note that the nested node_modules folder for workspace1 does not contain a symlink to workspace2, despite it being a dependency
  • note that eslint is still installed, despite being a dev dependency

Environment

  • npm: 9.7.1
  • Node.js: 16.16.0
  • OS Name: Windows 10 22H2
  • npm config:
; "user" config from C:\Users\<redacted>\.npmrc

<redacted internal repo>:_password = (protected)
<redacted internal repo>:email = "not-used@example.com"
<redacted internal repo>:username = <redacted>

; node bin location = C:\Program Files\nodejs\node.exe
; node version = v16.16.0
; npm local prefix = C:\S
; npm version = 9.7.1
; cwd = C:\S\npmrepro
; HOME = C:\Users\<redacted>
; Run `npm config ls -l` to show all defaults.

Activity

  1. added
    Bugthing that needs fixing
    Needs Triageneeds review for next steps
    Release 9.xwork is associated with a specific npm 9 release
    on Jun 8, 2023
  2. morganney commented on Jun 24, 2023

    @morganney

    I have experienced the same issue.

    @pcone to get around the --omit=dev issue you need npm >= 9.7.2.

    Another repo for reproducing: https://github.com/morganney/npm-workspaces

    If you run npm install --install-strategy=nested --omit=dev --workspace=foo there will be transitive dependencies for foo's dependencies inside of the root node_modules folder.

    I have created a workaround for this shortcoming by running npm run build:foo which effectively moves deps around from the associated node_modules directories.

  3. rmclaughlin-nelnet commented on Aug 14, 2023

    @rmclaughlin-nelnet

    I have also experienced this. I have also seen shared dependencies still being hoisted. node-fetch seems to be affected. We have a several dependencies that depend on it and no matter what npm install --install-strategy=nested installs it in the root, not the sub packages. npm install --install-strategy=linked seems to work better, but it is not optimal.

    It would be great to see a fix for this.

  4. iamyobi commented on Dec 21, 2023

    @iamyobi

    I have the same issue.
    npm 9.8.1
    node 18.18.2

    I removed node_modules

    • node_modules/
    • packages/PACAKGE/node_modules/

    and ran install command with install-strategy=nestedoption in package root, It doesn't help.

  5. requiel20 commented on Feb 6, 2024

    @requiel20

    Also affected by this

  6. r3ps4J commented on Apr 30, 2024

    @r3ps4J

    Same issue here, npm 10.5.2 on node v20.11.1. I can't used linked either because it breaks the angular compiler.

  7. benqus commented on May 15, 2024

    @benqus

    --install-strategy=nested does not work in npm@10.5.2 - I suggest this option to be taken out from the docs or at least mark it as such as it's misleading

  8. added a commit that references this issue on Jul 15, 2024
  9. added a commit that references this issue on Oct 3, 2024
  10. mystiker commented on Oct 11, 2024

    @mystiker

    You can still use install-strategy=nested:

    All you have to do is

    • running npm config set install-strategy="nested" to set this flag globally or
    • creating a .npmrc file that contains: install-strategy = "nested"

    It looks like cli parameters are not passed/parsed correctly.

    Environment variables are also possible according to The Docs but I didn't test that.

    I used npm@10.5.0

  11. hannupekka commented on Oct 11, 2024

    @hannupekka

    You can still use install-strategy=nested:

    All you have to do is

    • running npm config set install-strategy="nested" to set this flag globally or
    • creating a .npmrc file that contains: install-strategy = "nested"

    It looks like cli parameters are not passed/parsed correctly.

    Environment variables are also possible according to The Docs but I didn't test that.

    I used npm@10.5.0

    Does not work with npm@10.8.1. Dependencies are still hoisted to <root>/node_modules instead of <root>/package/node_modules

  12. QuentinLemCode commented on Oct 14, 2024

    @QuentinLemCode

    I have the same issue
    Even with install-strategy = "nested" in .npmrc
    And usgin npm i --install-strategy=nested -w packages/frontend

    All dependencies are installed in the root node_modules.

    The local node_modules only contains dependencies with conflicting version. However this doesn't work because one of my dependency is checking typescript version, and it use the typescript lib from the root node_modules instead of the local one.

  13. tanepiper commented on Nov 25, 2024

    @tanepiper

    Can confirm the same here:

    npm i --install-strategy=nested -w services/gateway

    image

    From this list of dependencies when I try to use buildpack, the ones that are missing are in <root>/node_modules while the ones found are in the correct workspace folder.

  14. tanepiper commented on Nov 25, 2024

    @tanepiper

    Possible workaround @pcone @hannupekka @QuentinLemCode - use npm ci instead

    npm ci --install-strategy=nested -w packages/frontend

    This seems to work

  15. hannupekka commented on Nov 26, 2024

    @hannupekka

    npm ci --install-strategy=nested -w

    Nope, atleast on my machine npm ci still hoists dependencies :(

  16. tanepiper commented on Nov 26, 2024

    @tanepiper

    npm ci --install-strategy=nested -w

    Nope, atleast on my machine npm ci still hoists dependencies :(

    Odd - using buildpack, it worked well after this. The only other difference it I put

    install-strategy = "nested" in .npmrc in the root, and in the workspace folder of the monorepo, e.g.

    /project/.npmrc
    /project/package/site/.npmrc
    

    After this, it's working well for me.

  17. mantoni commented on Jan 9, 2025

    @mantoni
    Contributor

    What makes it work for me is mkdir node_modules.

  18. ekymonthio commented on May 2, 2025

    @ekymonthio

    Still affected by this using npm@10.8.2

    None of the following works:

    • mkdir node_modules
    • passing the --workspace=false
    • .npmrc with install-strategy = "nested" in workspace root or package

    Any hints?

  19. bre30kra69cs commented on Aug 1, 2025

    @bre30kra69cs

    npm@10.9.2

    By some reason this work for me. Remove lock and node_modules before

    npm i --install-strategy=nested --legacy-peer-deps

    --install-strategy=shallow - better

  20. rmclaughlin-nelnet commented on Sep 24, 2025

    @rmclaughlin-nelnet

    npm i --install-strategy=nested --legacy-peer-deps

    Still not working for me on npm 10.9.2

  21. andyjv commented on Jan 6, 2026

    @andyjv

    The issue is that the install-strategy actually impacts how package-lock.json is initially generated; however it will not automatically re-generate package-lock.json if you install using a different strategy than what was used to generate the lock.

    If you delete package-lock.json, and re-generate using npm install --install-strategy=nested, the lock file will be generated "correctly", and the dependencies will be installed as described by the configuration. After that, all instances of npm install will default to using the nested strategy until the lock file is deleted and re-generated.

  22. added
    Priority 2secondary priority issue
    and removed
    Needs Triageneeds review for next steps
    on Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bugthing that needs fixingPriority 2secondary priority issueRelease 9.xwork is associated with a specific npm 9 release

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions