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.
-
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
1 Answer
Reset to default 3So 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 runningnpm 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 withnpm 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 of2
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.*