Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] npm link updates package.json to point to local directory #2034

Closed
Gameghostify opened this issue Oct 24, 2020 · 8 comments
Closed

[BUG] npm link updates package.json to point to local directory #2034

Gameghostify opened this issue Oct 24, 2020 · 8 comments
Labels
Bug thing that needs fixing Release 7.x work is associated with a specific npm 7 release

Comments

@Gameghostify
Copy link

Current Behavior:

npm link foo-bar updates package.json to point to the local directory of the foo-bar module instead of just symlinking it

Note that foo-bar is a private package installed from git (git+ssh)

Expected Behavior:

npm link foo-bar creates a symlink in node_modules/ without altering package.json

Steps To Reproduce:

  1.  $ cd ./foo-bar && npm link
    
  2.  $ cd ../other-package && (cat ./package.json | grep "foo-bar")
    
     "foo-bar": "git+ssh://[email protected]:..."
    
  3.  $ npm link foo-bar
    
  4.  $ cat ./package.json | grep "foo-bar"`
     
     "foo-bar": "file:../foo-bar",
    

The package foo-bar is now symlinked to the local foo-bar folder instead of the link in npm root -g

Environment:

  • OS: Ubuntu 20.04 (WSL)
  • Node: 12.9.0
  • npm: 7.0.5 (Has been happening with 7.0.0 - 7.0.5 though)
@Gameghostify Gameghostify added Bug thing that needs fixing Needs Triage needs review for next steps Release 7.x work is associated with a specific npm 7 release labels Oct 24, 2020
@Gameghostify Gameghostify changed the title [BUG] npm link updates package.json [BUG] npm link updates package.json to point to local directory Oct 24, 2020
@jmacura
Copy link

jmacura commented Oct 25, 2020

I confirm the same experience on Windows 10 1909 with NodeJS 15.0.1.
And I agree that such a behaviour seems undesired to me.

@wesleytodd
Copy link
Contributor

This example should be a full reduced repro:

$ git clone [email protected]:wesleytodd/english-days.git && cd english-days && npm link && cd .. # this is just a dep free test package I use
$ mkdir link-to && cd link-to && npm init -y && npm i english-days
$ cat package.json | grep english-days # "english-days": "^1.0.0"
$ npm link english-days
$ cat package.json | grep english-days # "english-days": "file:../english-days"

Also tried the above link command with --no-save which did not change the behavior and might be another bug.

@wesleytodd wesleytodd removed the Needs Triage needs review for next steps label Oct 26, 2020
@Gameghostify
Copy link
Author

Gameghostify commented Oct 26, 2020

Workarounds:

  1. You could just symlink it manually (ln -s ../foo-bar ./node_modules/foo-bar)
  2. You could do the following:
$ cd ./foo-bar && npm link
$ cd ../other-package
$ npm i --no-save "$(npm root -g)/foo-bar"

@ljharb
Copy link
Contributor

ljharb commented Oct 26, 2020

Unfortunately symlinking it manually like that won't set up any binary symlinks, nor will it run the appropriate lifecycle scripts.

@ankon
Copy link
Contributor

ankon commented Nov 11, 2020

Also tried the above link command with --no-save which did not change the behavior and might be another bug.

Trying with --no-save was also my first instinct when I saw that npm link modified package.json.

I think this is clearly a change in behavior that minimally needs to be documented in the release notes.

I'm relying on npm link for setting up a complex development environment, and as it stands now that simply won't work. Modifying package.json has a huge potential of me (or anyone using a similar setup) accidentally commiting the change in package.json, and breaking everything.

@Gameghostify
Copy link
Author

It would be great to have someone from NPM acknowledge this/give us some kind of a heads-up, I had to revert back to npm@6 because linking anything is a pain, especially when you have to do it often

ruyadorno added a commit to ruyadorno/cli that referenced this issue Nov 27, 2020
When running `npm link <pkg>` it should not save the new item to the
currrent dependencies of that package.json file.

Fixes: npm#2034
ruyadorno added a commit to ruyadorno/cli that referenced this issue Nov 27, 2020
When running `npm link <pkg>` it should not save the new item to the
currrent dependencies of that package.json file.

Fixes: npm#2034

Co-authored-by: Darcy Clarke <[email protected]>
@ruyadorno
Copy link
Contributor

Sorry about the delay @Gameghostify and thanks @wesleytodd for the handy reproduction 😊

This was fixed in 7.0.15, you can give it a try again with: npm install -g npm@7

@Gameghostify
Copy link
Author

@ruyadorno thank you for your hard work! It works now, which means I can finally use npm@7 for everything

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug thing that needs fixing Release 7.x work is associated with a specific npm 7 release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants