在尝试删除npm install创建的node_modules目录时:

源文件名大于该文件支持的文件名 系统。尝试移动到路径名称较短的位置,或者尝试 在尝试此操作之前,将名称重命名为较短的名称

我也尝试了shift + delete,仍然有同样的问题。


当前回答

我在windows 10上,我不能删除消息“目录不是空的”文件夹。rimraf和rm -rf都不起作用。

将一个空文本文件复制到每个文件夹中就可以了——我可以删除完整的node_modules文件夹。

其他回答

我使用的一个解决方案是:

(我更倾向于在使用CI环境时避免安装新的扩展(rimraf)。)

A) Rename packages.json to something else. B) Specially on CI - after npm install, I usually remove the file instead of renaming it, but if you need it, you don't have to do this. That's your choice. run npm init - this will create an empty packages.json file (no dependencies) run npm prune - this will match node_modules with the dependencies section of packages.json - which is now empty as the result of step #2. If you have chosen #1.A. step, delete the newly created packages.json, and rename original packages.json back to its original name.

PowerShell方式:

PS > rm -r -force node_modules

# The same, but without using aliases
PS > Remove-Item -Recurse -Force node_modules

如果你想删除子目录中的所有node_modules:

注意递归删除的潜在危险,请确保您在这里所做的事情

PS > dir -Path . -Filter node_modules -recurse | foreach {echo $_.fullname; rm -r -Force $_.fullname}

你可以使用另一个npm包来实现这个目的。 通过将其安装到本地文件夹using

NPM I rm-node-modules- recurur

如果你愿意,你也可以用-g开关全局安装它。

如果安装在本地,可以从当前目录node_modules.bin\ using调用二进制文件

 .\node_modules\.bin\rmnodes "<top level directory within quotes>"

如果您选择全局安装,只需使用

rmnodes "<top level directory within quotes>"

就是这样。这个包最终将轻松地递归地从所有子目录中删除node_modules(在我的例子中大约有60多个文件夹)。参考所附快照。 使用rm-node-modules-recur

我想之前没有提到过。但是删除不需要的node_modules的最好方法是安装一个名为npmkill的实用程序。

安装:

从您的终端:

npm i -g npkill

要使用它:

从您的终端:

npkill

或者,你可以直接使用它,而不需要安装:

npx npkill

然后你会看到一个项目列表,点击空格键可以删除它们的node_modules。

仅使用DOS命令DELETE,无需任何安装:

在C或D盘上创建一个空文件夹test,并使用以下DOS命令

robocopy /MIR c:\test D:\UserData\FolderToDelete > NUL

完成上述命令后,您的文件夹将为空,现在您可以删除文件夹。

不用担心,您的测试文件夹将始终为空,因此您可以随时删除它。