npm / npm/cli

[BUG] npm link and dependency hoisting don't play nice?

Open
#2,076 3 comments 2 reactions 1 assignee View on GitHub

@isaacs is already working on this.

Since Jan 22, 2021.

Bug Release 7.x
Dominant language
JavaScript
Stars
10.1k
Forks
4.7k
Avg merge
2d 2h
Merged PRs (30d)
19

Description

I am not 100% sure if this is a bug or I'm just misunderstanding the proper behaviour, but I don't really see a clear way to resolve it. I'm also having trouble building a minimal reproducing case because I don't seem to be able to play the npm link trick without first publishing the package I'm trying to cross-link.

Explanation:

Imagine a repo like this:

project-a/package.json:
  { "name": "project-a",
    "workspaces": [ "subproject-a" ] }
project-a/subproject-a/package.json:
  { "name": "subproject-a",
    "dependencies": { "webpack-merge": "^4.2.2" }}
project-b/package.json
  { "name": "project-b",
    "dependencies": { "subproject-a": "0.0.1" }}

I want to do local development on everything together, so I do:

( cd project-a/subproject-a && npm link )
cd project-b
npm link subproject-a
npm install

If I do something in project-b that requires webpack-merge, it generally won't be able to find the dependency, because it isn't anywhere in the upstream of subproject-a as it appears in project-b/node_modules.

I also observed the issue (already logged) that npm link updates project-b/package.lock with a file: reference.

EDIT: Also note that if I break ranks and run npm install in subproject-a directly, everything works according to plan.

Versions:

npm@7.0.6

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.