是否有删除所有全局npm模块的命令?如果没有,你有什么建议?


当前回答

好吧,如果你在windows上,想要删除/卸载所有的node_modules,那么你需要执行以下步骤。

进入windows命令提示符 导航到node_modules目录(不在node_modules文件夹内) 输入下面的命令,并给它1-2分钟,它将卸载node_module中的所有目录 Rmdir /s /q node_modules .使用实例

希望这对窗户上的人有所帮助

其他回答

对于Windows,这个脚本可以用来核化本地和用户的全局模块和缓存。

我注意到在linux上,全局根对系统来说是全局的,而不是给定的用户。因此,对于共享系统,删除全局根可能不是一个好主意。除此之外,如果有兴趣,我可以将脚本移植到bash。

对于Windows,保存到一个cmd文件中运行。

@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%

你可以在以下位置找到你所有安装的npm包:

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

并删除你想删除的NPM内容。

如果AppData没有显示,这意味着它被隐藏了,你可以在文件资源管理器中查看并选中隐藏项,然后你可以看到所有隐藏的文件夹。

对于一种更手动的方法,它不涉及文件浏览器,不关心安装在哪里,以后不太可能崩溃,100%跨平台兼容,并且由于额外的步骤而感觉更安全,请使用这种方法。

npm ls -g --depth=0 Copy output Paste into favorite code editor (I use vsCode. Great multi-cursor editing) Check for any packages you'd like to keep (nodemon, yarn, to name a few) Remove those lines Remove every instance of +-- or other line decorators Remove all the version information (eg '@2.11.4') Put all items on same line, space separated Add npm uninstall -g to beginning of that one line. Mine looks like npm uninstall -g @angular/cli @vue/cli express-generator jest mocha typescript bindings nan nodemon yarn, but I didn't install many packages globally on this machine. Copy line Paste in terminal, hit enter if not already added from the copy/paste Look for any errors in the terminal. Check npm ls -g to make sure it's complete. If something got reinstalled, rinse and repeat

其他仅使用cli的方法非常适合计算机管理员从相同的ssh或Puppet同时为100台几乎相同的计算机做一些事情。但如果你只做一次,甚至一年5次,这就容易多了。

对于那些使用Windows的人来说,删除所有全局安装的npm包的最简单的方法是删除以下内容:

C:\用户\用户名\应用数据\漫游\npm

您可以通过在资源管理器、运行提示符或开始菜单中键入%appdata%/npm快速到达那里。

因为这是搜索中最热门的答案,所以我把它贴在这里,因为这是我过去用来清洁电脑切换笔记本电脑的解决方案。

cd ~/Documents # or where you keep your projects
find . -name "node_modules" -exec rm -rf '{}' +

来源:https://winsmarts.com/delete-all-node-modules-folders-recursively-on-windows-edcc9a9c079e