npm Community Forum (Archive)

The npm community forum has been discontinued.

To discuss usage of npm, visit the GitHub Support Community.

`npm unlink foo` doesn't reinstall the "real" `foo`

Imported from https://github.com/npm/npm/issues/20995.

I’m opening this issue because:

npm is producing incorrect or undesirable behavior.

What’s going wrong?

npm unlink foo doesn’t reinstall the “real” foo. Even worse, it modifies my package.json to completely remove foo. To fix these issues, I have to do npm unlink --no-save foo && npm install.

This behavior is confusing because it means that npm unlink is not a mirror of npm link:

I realize that this behavior occurs because npm unlink is merely an alias for npm uninstall, as shown by running npm unlink -h. But I do not think it should be an alias, but rather its own command with its own behavior.

It seems like this is a partial design goal for you folks per https://github.com/npm/npm/issues/6248, "npm link and npm unlink should be mirror operations"… but I don’t understand how you can consider them mirror operations if npm unlink === npm uninstall.

TL;DR npm unlink foo should made to behave like npm unlink --no-save foo && npm install, to make npm unlink foo the mirror of npm link ../foo.

How can the CLI team reproduce the problem?

As per the commands in the previous section, s/foo/any-real-package. I am glad to provide a test package if necessary.

supporting information:


Interestingly, it appears that if I do npm unlink ../foo, then the real module will be reinstated, and not removed from package.json, although npm may still make other modifications to package.json like alphabetizing the dependencies.

TL;DR npm unlink ../foo appears to be a shorter workaround than npm unlink --no-save foo && npm install.


The entire story about linking/unlinking/local files/temp linking/etc is going to change enormously once the new link RFC is ratified and implemented, so I’d suggest giving feedback in that RFC to make sure your use case is covered if you don’t think it currently is:

https://npm.community/t/new-npm-link-command/24/2


That RFC looks like it’ll do the trick, thanks @zkat!