全局删除所有npm模块的命令?

Is there a command to remove all global npm modules? If not, what do you suggest?

#0

The following command removes all global npm modules. Note: this does not work on Windows. For a working Windows version, see Ollie Bennett's Answer.

npm ls -gp --depth=0 | awk -F/ '/node_modules/ && !/\/npm$/ {print $NF}' | xargs npm -g rm

Here is how it works:

  • npm ls -gp --depth=0 lists all global top level modules (see the cli documentation for ls)
  • awk -F/ '/node_modules/ && !/\/npm$/ {print $NF}' prints all modules that are not actually npm itself (does not end with /npm)
  • xargs npm -g rm removes all modules globally that come over the previous pipe

#1

For those using Windows, the easiest way to remove all globally installed npm packages is to delete the contents of:

C:\Users\username\AppData\Roaming\npm

You can get here quickly by typing %appdata% (either in explorer, run prompt, or start menu).

#2

I tried Kai Sternad's solution but it seemed imperfect to me. There was a lot of special symbols left after the last awk from the deps tree itself.

So, I came up with my own modification of Kai Sternad's solution (with a little help from cashmere's idea):

npm ls -gp --depth=0 | awk -F/node_modules/ '{print $2}' | grep -vE '^(npm|)$' | xargs -r npm -g rm

npm ls -gp --depth=0 lists all globally-installed npm modules in parsable format:

/home/leonid/local/lib
/home/leonid/local/lib/node_modules/bower
/home/leonid/local/lib/node_modules/coffee-script
...

awk -F/node_modules/ '{print $2}' extracts module names from paths, forming the list of all globally-installed modules.

grep -vE '^(npm|)$' removes npm itself and blank lines.

xargs -r npm -g rm calls npm -g rm for each module in the list.

Like Kai Sternad's solution, it'll only work under *nix.

#3

sudo npm list -g --depth=0. | awk -F ' ' '{print $2}' | awk -F '@' '{print $1}'  | sudo xargs npm remove -g

worked for me

  • sudo npm list -g --depth=0. lists all top level installed
  • awk -F ' ' '{print $2}' gets rid of ├──
  • awk -F '@' '{print $1}' gets the part before '@'
  • sudo xargs npm remove -g removes the package globally

#4

For those using Powershell:

npm -gp ls --depth=0 | ForEach-Object { Get-Item $_ } | Where { $_.Name -ne 'npm' } | ForEach-Object { npm rm -g $_.Name }

To clear the cache:

npm cache clear

#5

Just switch into your %appdata%/npm directory and run the following...

for package in `ls node_modules`; do npm uninstall $package; done;

EDIT: This command breaks with npm 3.3.6 (Node 5.0). I'm now using the following Bash command, which I've mapped to npm_uninstall_all in my .bashrc file:

npm uninstall `ls -1 node_modules | tr '/\n' ' '`

Added bonus? it's way faster!

https://github.com/npm/npm/issues/10187

How do you uninstall all dependencies listed in package.json (NPM)?

#6

If you would like to remove all the packages that you have installed, you can use the npm -g ls command to find them, and then npm -g rm to remove them.

#7

in windows go to "C:\Users{username}\AppData\Roaming" directory and manually remove npm folder

#8

If you have jq installed, you can go even without grep/awk/sed:

npm ls -g --json --depth=0 |
  jq -r '.dependencies|keys-["npm"]|join("\n")' |
  xargs npm rm -g

On Debian and derived you can install jq with:

sudo apt-get install jq

#9

OS not specified by OP. For Windows, this script can be used to nuke the local and the user's global modules and cache.

I noticed on linux that the global root is truly global to the system instead of the given user. So deleting the global root might not be a good idea for a shared system. That aside, I can port the script to bash if interested.

For Windows, save to a cmd file to run.

@ECHO OFF
SETLOCAL EnableDelayedExpansion 
SETLOCAL EnableExtensions

SET /A ecode=0

:: verify
SET /P conf="About to delete all global and local npm modules and clear the npm cache. Continue (y/[n])?
IF /I NOT "%conf%"=="y" (
  ECHO operation aborted
  SET /A ecode=!ecode!+1
  GOTO END
)

:: wipe global and local npm root
FOR %%a IN ("" "-g") DO (

  :: get root path into var
  SET cmd=npm root %%~a
  FOR /f "usebackq tokens=*" %%r IN (`!cmd!`) DO (SET npm_root=%%r)

  :: paranoid
  ECHO validating module path "!npm_root!"
  IF "!npm_root:~-12!"=="node_modules" (
    IF NOT EXIST "!npm_root!" (
      ECHO npm root does not exist "!npm_root!"
    ) ELSE (
      ECHO deleting "!npm_root!" ...
      :: delete
      RMDIR /S /Q "!npm_root!"
    )
  ) ELSE (
      ECHO suspicious npm root, ignoring "!npm_root!"
  )
)

:: clear the cache
ECHO clearing the npm cache ...
call npm cache clean

:: done
ECHO done

:END

ENDLOCAL & EXIT /b %ecode%

#10

Use this code to uninstall any package:

npm rm -g <package_name>

#11

All you done good job. This is combined suggestions in to one line code.

npm rm -g `npm ls -gp --depth=0 | awk -F/node_modules/ '{print $2}' | tr '/\n' ' '`

What is different? Uninstall will be done in single command like: npm rm -g *** *** ***

#12

if you have Intellij Webstorm you can use its built-in graphical package manager.

open it as root and create an emtpy project. go to

File > Settings > Language and Frameworks > Node.js and NPM

there you will see all the installed packages. Uninstalling is easy, you can select and deselect any package you want to uninstall, Ctrl+a woks as well.

#13

Well if you are on windows, and want to remove/uninstall all node_modules then you need to do following steps.

  1. Go to windows command prompt
  2. Navigate to node_modules directory (Not inside node_modules folder)
  3. Type below command and give it for 1-2 minutes it will uninstall all directories inside node_module

     rmdir /s /q node_modules
    

Hope this will help some one on windows

#14

npm ls -gp | awk -F/ '/node_modules/&&!/node_modules.*node_modules/&&!/npm/{print $NF}' | xargs npm rm -g

推荐文章

Ajax加载器和事件运行不止一次

Ajax加载器和事件运行不止一次

推荐文章

CBuilder中的HTTP服务器

CBuilder中的HTTP服务器

推荐文章

为什么灰色像素出现在黑色s:线的边缘?

为什么灰色像素出现在黑色s:线的边缘?

推荐文章

Android自定义微调器行(照片+字符串)

Android自定义微调器行(照片+字符串)

推荐文章

Rsyslog+Virtualenv

Rsyslog+Virtualenv

推荐文章

按端点调整iphone UIImage大小

按端点调整iphone UIImage大小

推荐文章

将图像重写为S3

将图像重写为S3

推荐文章

如何向数组中添加新元素

如何向数组中添加新元素

推荐文章

太阳黑子轨道宝石-“Errno::econnrefured(连接被拒绝-连接(2))”

太阳黑子轨道宝石-“Errno::econnrefured(连接被拒绝-连接(2))”

推荐文章

创建Firefox选项卡溢出效果

创建Firefox选项卡溢出效果

推荐文章

Android上Facebook应用的语言设置

Android上Facebook应用的语言设置

推荐文章

Coldfusion-清除和附加数组

Coldfusion-清除和附加数组

推荐文章

分离编辑器视图

分离编辑器视图

推荐文章

Android NDK:NDK构建脚本失败-脚本“check cygwin”-制造商.mk“找不到

Android NDK:NDK构建脚本失败-脚本“check cygwin”-制造商.mk“找不到

推荐文章

对列表中的元素进行更简洁的赋值(Python)?

对列表中的元素进行更简洁的赋值(Python)?

推荐文章

如何缩短jquery代码?

如何缩短jquery代码?