简体   繁体   中英

npm unlink all linked global modules

I can run this to list all npm modules I have linked globally via npm link... :

npm ls -g --depth=0 --link=true

So how do I just remove/unlink all of those linked modules? I have over 10 dependencies, so I'm looking for something more palatable than running npm unlink [module] 10 times.

So there are two parts of npm link: the source package and the destination. On the source package, you run npm link and a symlink is created in the global node_modules folder as described here :

$ {prefix}/lib/node_modules/<package>

The {prefix} can be found by running npm prefix -g - which we will use below.

The global node_modules contains all globally installed modules, so we can't just empty that folder. Instead we need to find all symlinks and delete them. Here's the command to do that (should work on mac/linux):

find $(npm prefix -g)/lib/node_modules -maxdepth 2 -type l -delete

Here's a brief rundown of what's going on:

  • the -type l filters only symlinks
  • The -maxdepth 2 is necessary to only target packages that are linked with npm link . If you remove that filter you will see that there are a bunch of global bin-links and other symlinks which we do not want to touch. We need a depth of 2 to cover @scoped/* and unscoped packages.
  • The -delete at the end is the "action" which is performed on the found files. You can remove it and it will fallback to the default -print command, which will list all the found files without deleting them.

On the destination package, this is where you run npm link <package> which creates a local symlink pointing to the global one created above. Unfortunately, there's no easy way to find all of these "local" symlinks without searching an entire directory tree. The easiest thing to do is run npm unlink <package> in any of the destination packages, but this is very manual. You are free to play around with the find command and see what you can muster. I'd start with this, which will find all symlinks in the current user's home directory AND within a node_modules folder:

find ~/ -type l -regex .*node_modules.*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM