最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - npm unlink all linked global modules - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question asked Oct 4, 2018 at 21:47 SeanSean 1,2281 gold badge17 silver badges21 bronze badges 3
  • 3 In linux something like npm ls -g --depth=0 --link=true | xargs npm unlink might work – apokryfos Commented Oct 4, 2018 at 21:50
  • docs.npmjs./cli/link – Dženis H. Commented Oct 5, 2018 at 1:02
  • thanks for including the --link=true param that I didn't know about. very helpful. – wraiford Commented Oct 25, 2023 at 11:08
Add a ment  | 

1 Answer 1

Reset to default 3

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 mand 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 mand, 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 mand 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.*
发布评论

评论列表(0)

  1. 暂无评论