是否有删除所有全局npm模块的命令?如果没有,你有什么建议?
当前回答
在macOS中,我相信你可以简单地删除用户目录中的.npm-global文件夹。
macOS下的.npm和.npm-global文件夹用户目录:
其他回答
如果你想删除你已经安装的所有包,你可以使用npm -g ls命令找到它们,然后npm -g rm删除它们。
对于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为我做所有的工作。
# On Linux Mint 19.1 Cinnamon
# First navigate to where your global packages are installed.
$ npm root # returns /where/your/node_modules/folder/is
$ cd /where/your/node_modules/folder/is # i.e for me it was cd /home/user/.npm-packages/lib/node_modules
然后,如果你进行npm卸载或npm删除,这些模块将被视为正常的项目依赖项。它甚至生成一个包锁。Json文件,当它完成:
$ npm remove <package-name> # you may need sudo if it was installed using sudo
如果你有Intellij Webstorm,你可以使用它内置的图形包管理器。
以root用户打开并创建一个空项目。去
>文件设置>语言和框架> Node.js和NPM
在那里您将看到所有已安装的包。卸载很容易,你可以选择和取消选择任何包,你想卸载,Ctrl+一个工作以及。
对于一种更手动的方法,它不涉及文件浏览器,不关心安装在哪里,以后不太可能崩溃,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次,这就容易多了。
推荐文章
- CALL_AND_RETRY_LAST分配失败-进程内存不足
- 在Ubuntu上安装Node.js
- 使用express.js代理
- Node -使用NODE_MODULE_VERSION 51根据不同的Node.js版本编译
- RabbitMQ / AMQP:单队列,同一消息的多个消费者?
- Node.js同步执行系统命令
- 禁用包的postinstall脚本
- Node.js上的html解析器
- 错误:无法找到模块“webpack”
- 在node.js中使用async / await文件系统
- NodeJS -用NPM安装错误
- 如何为本地安装npm包设置自定义位置?
- 回调函数来处理管道的完成
- Express函数中的“res”和“req”参数是什么?
- node.js TypeError:路径必须是绝对路径或指定根路径到res.sendFile[解析JSON失败]