252

When installing a node package using sudo npm link in the package's directory, how can I uninstall the package once I'm done with development?

npm link installs the package as a symbolic link in the system's global package location ('/usr/local/lib`). This allows you to test the package while still developing it, without having to install it over and over again.

Which npm command do I need to run to remove the link again?

allicarn
  • 2,859
  • 2
  • 28
  • 47
nwinkler
  • 52,665
  • 21
  • 154
  • 168

8 Answers8

267

The package can be uninstalled using the same uninstall or rm command that can be used for removing installed packages. The only thing to keep in mind is that the link needs to be uninstalled globally - the --global flag needs to be provided.

In order to uninstall the globally linked foo package, the following command can be used (using sudo if necessary, depending on your setup and permissions)

sudo npm rm --global foo

This will uninstall the package.

To check whether a package is installed, the npm ls command can be used:

npm ls --global foo
nwinkler
  • 52,665
  • 21
  • 154
  • 168
  • 15
    If you aren't sure the name of the linked package you can also do `npm ls --global --depth 0` to list all your top level dependencies – schu34 Nov 30 '17 at 18:20
  • 1
    This didn't work for me because of permission issues, even with `sudo`. I created the missing directory with a dummy `package.json` with the `name` and `version` field (`name` should match the package name, `version` can be anything). Inside that folder I ran `npm link && npm unlink` and it worked. – silvenon Feb 02 '18 at 15:11
  • `npm uninstall` from the local library folder will also work. – backslashN Jul 07 '20 at 10:06
  • 1
    `sudo` isn't necessary if you're using something like [nvm](https://github.com/nvm-sh/nvm) to install node – bmaupin Sep 28 '21 at 15:10
  • 2
    `npm list -g --depth=1` worked for me – Neil Oct 15 '21 at 16:36
  • With latest version of node you should use: `npm rm --location=global foo` – Manjar Sep 06 '22 at 07:30
176

you can use unlink to remove the symlink.

For Example:

cd ~/projects/node-redis 
npm link                 
cd ~/projects/node-bloggy
npm link redis             # links to your local redis

To reinstall from your package.json:

npm unlink redis
npm install

https://www.tachyonstemplates.com/npm-cheat-sheet/#unlinking-a-npm-package-from-an-application

Blair Anderson
  • 19,463
  • 8
  • 77
  • 114
32

npm link pain:

-Module name gulp-task

-Project name project-x


You want to link gulp-task:

1: Go to the gulp-task directory then do npm link this will symlink the project to your global modules

2: Go to your project project-x then do npm install make sure to remove the current node_modules directory


Now you want to remove this madness and use the real gulp-task, we have two options:

Option 1: Unlink via npm:

1: Go to your project and do npm unlink gulp-task this will remove the linked installed module

2: Go to the gulp-task directory and do npm unlink to remove symlink. Notice we didn't use the name of the module

3: celebrate


What if this didn't work, verify by locating your global installed module. My are location ls -la /usr/local/lib/node_modules/ if you are using nvm it will be a different path


Option 2: Remove the symlink like a normal linux guru

1: locate your global dependencies cd /usr/local/lib/node_modules/

2: removing symlink is simply using the rm command

rm gulp-task make sure you don't have / at the end

rm gulp-task/ is wrong

rm gulp-task ✔️

KhaledMohamedP
  • 5,000
  • 3
  • 28
  • 26
6

This worked for me:

  1. check the list of npm global packages:

    npm ls --global

  2. uninstall your package:

    npm uninstall --global my-package

  3. go to your testbed and unlink the linked package:

    npm unlink my-package

  4. navigate to your testbed directory and reinstall packages:

    npm install

  5. restart your testbed server

5

npm uninstall --global my-package

JulianSoto
  • 182
  • 1
  • 4
  • 15
  • See "[Explaining entirely code-based answers](https://meta.stackoverflow.com/q/392712/128421)". While this might be technically correct, it doesn't explain why it solves the problem or should be the selected answer. We should educate along with helping solve the problem. – the Tin Man Mar 22 '22 at 05:13
2

You can undo the link command with the unlink command.

Create a link

In package

cd ./some-package
npm link 

In comsumer

cd ./some-project 
npm link some-package 

Remove a link

Removing a link should be done in the reverse order - start from the consumers.

cd ./some-project 
npm unlink some-package 

In package

cd ./some-package
npm unlink 
Ben Carp
  • 24,214
  • 9
  • 60
  • 72
  • 1
    If you used link to override an existing package this will leave you without the published version of the package. You need to do `npm unlink some-package && npm install some-package`. Even then you'll still need to remove the global package with `npm rm --global some-package` – akaltar Jul 15 '22 at 14:05
1

If you've done something like accidentally npm link generator-webapp after you've changed it, you can fix it by cloning the right generator and linking that.

git clone https://github.com/yeoman/generator-webapp.git;
# for fixing generator-webapp, replace with your required repository
cd generator-webapp;
npm link;
Code Whisperer
  • 22,959
  • 20
  • 67
  • 85
1

"npm install" replaces all dependencies in your node_modules installed with "npm link" with versions from npmjs (specified in your package.json)

Eugenio
  • 563
  • 5
  • 8
  • 1
    The OP asked about how to uninstall packages installed with `npm link` not `npm link somepackage`. what you suggest would leave the package at the global node_modules. – kroiz Aug 19 '21 at 17:51