Repository navigation
Description
---- EDIT 2021-05-10: Added how to reproduce in comment below.
npm install replaces link with downloaded module
Current Behavior
After running npm link, a symlink is created to the target dependency. Afterwards, other npm commands such as npm install will break the mentioned link:
# ("openvidu-browser" is a global link that has
# been previously created with `sudo npm link`...)
$ npm link openvidu-browser
$ ls -l node_modules | grep openvidu-browser
openvidu-browser -> ../../../openvidu-generateOffer/openvidu-browser
# (Any other install command)
$ npm install --no-save @ionic/cli
# Show the bug: "openvidu-browser" is now a downloaded dependency (following what is defined in package.json)
# instead of the expected symlink to a local version of the dependency.
$ ls -l node_modules | grep openvidu-browser
openvidu-browser # UNEXPECTED: NOT A LINKExpected Behavior
After running npm link, a symlink is created to the target dependency. Afterwards, other npm commands such as npm install don't alter the mentioned link:
# ("openvidu-browser" is a global link that has
# been previously created with `sudo npm link`...)
$ npm link openvidu-browser
$ ls -l node_modules | grep openvidu-browser
openvidu-browser -> ../../../openvidu-generateOffer/openvidu-browser
# (Any other install command)
$ npm install --no-save @ionic/cli
# Show the expected behavior: "openvidu-browser" is still a symlink to a local version of the dependency.
$ ls -l node_modules | grep openvidu-browser
openvidu-browser -> ../../../openvidu-generateOffer/openvidu-browserDescription
For context, this is a well-known and long-standing bug that has been causing confusion to users and has been reported multiple times in the past. Here are some relevant or related reports:
-
npm install after npm link deletes linked module dependencies in it's node_modules folder #17287. Repo archived. Issue ignored. Closed without a fix, for a future overhaul on npm@7.
-
npm installafternpm linkwill "steal" dependencies from linked packages #10343. Repo archived. Issue ignored. Closed without a fix, for a future overhaul on npm@7. -
npm 5 dedupes even in linked modules #16788. Still open but repo archived.
-
Npm deletes stuff from linked module’s node_modules. Again, forums were archived.
-
Sometimes
npm installdeletes linked module, replaces it with downloaded module.
Some of these reports were closed with the conclusion that the issue would definitely not be fixed in current versions (<= 6) of NPM, and instead a fix would be postponed for a future NPM v7, citing "an overhaul of how npm link works" and this RFC.
After bumping into this bug today, recollecting all this information, and updating my NPM version to 7.2.0, I was surprised to find that links are still lost when any further install operation is done after running npm link.
So I guess the question is: was this really fixed with NPM 7 and is now a regression?
Environment:
- OS: Ubuntu 18.04
- Node: Tested on v14 and v15
- npm: 7.2.0
Activity
j1elo commentedon Dec 17, 2020
In case this could be helpful for anyone, I was able to work around this issue by using
npm install <path>instead ofnpm link:nlf commentedon Feb 3, 2021
we've made some changes with regards to how linked modules are handled, can you try updating to the latest release
7.5.2and let us know if you still experience this problem?IronGeek commentedon Apr 30, 2021
This should be reopen, the bug still not fixed (or maybe regressed) on npm 7.11.2
j1elo commentedon May 10, 2021
I agree this shouldn't have been closed, unless the issue had maybe been fixed and now it is showing up again as a regression. Anyway, this bug is not an intricate or difficult to reproduce one, at all; actually if anything it is pretty trivial to check. So much, that I'm surprised this is not having a bigger impact on lots of NPM users. Are we just using
npm linkwrong?For future reference I'm adding here a complete set of example steps that can be followed to see the problem in action; these can be used at any future time to re-check if the problem persists.
2 remaining items
ljharb commentedon May 10, 2021
afaik, since npm 5, this is how
npm installhas worked. It's annoying, but it's by design.Any time you install something, you have to manually recreate all your links.
Venryx commentedon May 10, 2021
It's not by design. The NPM team has talked about it before, and said npm 7 was going to fix it, but then it never was fixed:
The comment above was made three years ago...
(The original post of the linked thread is not exactly the same issue -- it's about
npm install"stealing" subdependencies from npm-linked dependencies rather than outright replacing the npm-linked dependencies -- but it's a similar issue, and those contributing to the thread were referencing both kinds of misbehavior. This is true also for the thread for the merged pull-request, which people were hoping was going to fix it.)ljharb commentedon May 10, 2021
That there's tons of issues with it, and that the plan is to fix it, does not mean it wasn't originally by design.
Venryx commentedon May 18, 2021
If this issue is on the npm dev team's radar, can we get an update on the roadmap/plan for its resolution?
npm linkis a core feature for me and many other projects (for example, I use linked packages all the time, for almost every app I work on), and that this critical bug has existed for over three years (requiring me to always use the 3rd-party tool npm-safe-install just to install new packages) makes me think the dev-team may be:Whichever of those three is the case, the lack of response on it is concerning, as it gives the impression that the dev team may not have the resources/motivation to maintain the command's functionality/reliability.
I've been annoyed by this issue for enough months/years that I'm finally seriously considering switching to Yarn now (despite most of Yarn's previous advantages having been "caught up to" by npm).
That today I learned about Yarn's plugin system (which NPM doesn't have an equivalent of, due partly to NPM's file-based hook scripts not working on Windows) has pushed me right to the edge. (I'm torn between NPM's ubiquity, and Yarn's solving of a few key "npm pain points".)
A resolution would be ideal; but I'll settle for just a dev acknowledgement + discussion at this point!
SystemParadox commentedon May 22, 2021
If this is still broken I would suggest opening a new ticket to have any chance of being noticed.
j1elo commentedon May 22, 2021
That'd be a full-circle closure for this issue, I have to admit. A self-fulfilling prophecy where this report ends up joining the long trail of closed and/or forgotten others that are listed in the initial description.
If anyone opens a new issue, it might be useful that the past reports research that I did was copied over too, so the full context doesn't get lost. Good luck with that, though; seems their policy is to close well documented reports (like this one) even if the issue is well known, trivial to reproduce, and regardless of whether it is actually fixed or not. I don't care any more to follow up with more reporting.
rektide commentedon Jul 4, 2021
@nlf @darcyclarke can we please re-open this issue? this is still present. i feel like the in-depth write-up of this ticket makes this extra-worthy of re-opening versus opening another ticket.
Venryx commentedon Aug 17, 2021
Yet again,
npm installafter a link being present caused the directory that was symlinked to be completely replaced with the latest NPM release. If I did not have online Git backups of that library, I would have lost all progress on it since its last npm release.Note that not only does the library's working-directory get nuked -- literally everything inside the symlinked folder gets nuked, including the
.gitfolder. So having Git backups is not enough, you need them to also be online, to keepnpm install(afternpm link) from potentially destroying everything in the symlinked folder permanently. (I'm not sure of the exact conditions which will trigger this, but it has happened several times now)Beware of using
npm link my-library, without a (frequently updated) online copy of the library!(FWIW, I've since switched to using yarn for all my new projects; haven't converted everything to it yet though. For people annoyed by Yarn 2's inability to have user-specific symlinks, I made a Yarn plugin that adds support for this.)
SystemParadox commentedon Aug 17, 2021
@Venryx WOAH really?
This bug is about the links themselves being deleted. If npm is reaching through the link and deleting files inside the linked directory then please open a separate issue as that's incredibly serious and the npm team doesn't seem to be paying much attention to this one.
isaacs commentedon Aug 17, 2021
Re: #2372 (comment) (et al)
The issue with the link being removed on a subsequent
npm installis owing to the very much by design (and, I agree, confusing and weird) behavior thatnpm linkdoes not by default save package.json or package-lock.json. There was a fair bit of discussion around this at the time (back in 2017 or so, I believe?) but it is a thing we intend to thoroughly review and significantly change in a future semver-major update. We did not opt to change it in npm v7 for the simple fact that we already were adding some other rather large changes, and wanted to limit the amount of disruption between v6 and v7 (which meant preserving some behaviors that are questionable at best).As @j1elo mentioned, the workaround for this is to explicitly install a path with
npm install file:/some/path/to/thing, which will create a symbolic link and save the dependency to the package.json and lockfile.Another workaround is to set
--saveexplicitly when runningnpm link. So, for example, instead ofnpm link openvidu-browser, you'd donpm link openvidu-browser --save.@Venryx This is a different issue than the OP in this issue. Can you please open a new issue, and provide as much reproduction details as possible? At the very least, operating system and versions of npm and node in use. The best would be a minified reproduction case that shows the problem. That one has been reported a few times, and it's proven very tricky to track down, but it is possible that I'm about to fix it, since it seems like it might (in theory) match the pattern of another issue I'm working on. Thanks!
thw0rted commentedon Aug 18, 2021
@isaacs -- it sounds like there isn't a path forward for
npm linkto just DWIM.linkis necessarily a "local" operation, against the system where I ran the command, and no other. I'd make the strong argument thatlinkshould never make a change that winds up in source control -- that, or I fundamentally misunderstand howlinkis meant to be used. At the same time, I wantlinkchanges to persist until Iunlink. I wantlink/unlinkto be a mode-switch between "use the repo version of this package" and "use this local directory in place of the package".I'm not a Node/npm engineer, but if I were designing this feature I'd want to persist
linkchanges in a place other thanpackage{-lock}.json, ideally something that we tell everybody not to check into source control -- something like~/.npmrc. (There certainly might be reasons why that's a terrible solution, it's just the first place I could think of that meets the criteria.)joshjung commentedon Sep 26, 2021
FWIW, my suggestion on this issue would perhaps be to have a
package.local.jsonthat could be ignored by the repo. Every time one does anpm linkthis file would be updated with the locally linked package so that whennpm iis run in the future it can relink everything in that localpackage.json. This would also allow a lot of scalability for "local only" npm settings in the future.mildsunrise commentedon Jul 24, 2024
it's been 3 years and 3 new major versions, and this has AFAIK not been addressed.
can we at least please reopen this? @isaacs
isaacs commentedon Jul 24, 2024
I no longer work on npm, haven't for about three years now. Please don't ping me on these issues, I can't help you.