如何卸载包中列出的所有依赖项。json(NPM)?

时间:2023-01-19 18:29:25

If I have a package.json file defined in my application root and run npm install -g it will install all the dependencies defined in package.json, globablly.

如果我有一个包裹。在我的应用程序根中定义的json文件并运行npm安装-g它将安装包中定义的所有依赖项。json,globablly。

However, this doesn't seem to work in reverse.

然而,这似乎并不是反过来的。

If I do npm uninstall -g in my application root it throws an error, expceting me to pass it a package name.

如果我在我的应用程序根中卸载-g,它会抛出一个错误,使我将一个包名传递给它。

Shouldn't this also uninstall the same packages I installed?

这不也应该卸载我安装的相同的包吗?

Am I doing something wrong?

我做错什么了吗?

7 个解决方案

#1


124  

If using Bash, just switch into the folder that has your package.json file and run the following:

如果使用Bash,请切换到包含包的文件夹。json文件并运行如下:

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

In the case of globally-installed packages, switch into your %appdata%/npm folder (if on Windows) and run the same command.

在全局安装包的情况下,切换到%appdata%/npm文件夹(如果在Windows上)并运行相同的命令。

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 3.3.6(节点5.0)时中断。我现在正在使用下面的Bash命令,我已经将它映射到.bashrc文件中的npm_uninstall_all:

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

Added bonus? it's way faster!

额外的好处?这是更快!

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

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

#2


42  

This worked for me:

这工作对我来说:

command prompt or gitbash into the node_modules folder in your project then execute:

命令提示符或gitbash进入项目中的node_modules文件夹,然后执行:

npm uninstall *

Removed all of the local packages for that project.

删除该项目的所有本地包。

#3


27  

For windows go to node_modules dir and run this in powershell

对于windows,转到node_modules目录并在powershell中运行它

npm uninstall (Get-ChildItem).Name 

#4


11  

I recently found a node command that allows uninstalling all the development dependencies as follows:

我最近发现了一个节点命令,它允许卸载所有开发依赖项,如下所示:

npm prune --production

As I mention, this command only uninstalls the development dependency packages. At least it helped me not to have to do it manually.

如前所述,此命令只卸载开发依赖包。至少它帮助我不必手动操作。

#5


8  

Actually there is no option to do that, if you want to uninstall packages from package.json simply do npm ls on the same directory that package.json relies and use npm uninstall <name> or npm rm <name> for the package you want to remove.

实际上,如果您想要从包中卸载包,就没有这个选项。json仅在包所在的目录上执行npm ls。json依赖并使用npm卸载 <名称> 或npm rm <名称> ,用于您想要删除的包。

#6


4  

Tip for Windows users: Run this PowerShell command from within node_modules parent directory:

Windows用户提示:在node_modules父目录中运行这个PowerShell命令:

ls .\node_modules | % {npm uninstall $_}

#7


1  

// forcibly remove and reinstall all package dependencies
ren package.json package.json-bak
echo {} > package.json
npm prune
del package.json
ren package.json-bak package.json
npm i

This essentially creates a fake, empty package.json, calls npm prune to remove everything in node_modules, restores the original package.json and re-installs everything.

这实际上创建了一个虚假的空包。json,调用npm prune删除node_modules中的所有内容,重新存储原始包。json和重新安装。

Some of the other solutions might be more elegant, but I suspect this is faster and exhaustive. On other threads I've seen people suggest just deleting the node_modules directory, but at least for windows, this causes npm to choke afterward because the bin directory goes missing. Maybe on linux it gets restored properly, but not windows.

其他的一些解决方案可能更优雅,但我怀疑这是更快和详尽的。在我看到的其他线程中,人们建议删除node_modules目录,但至少对于windows来说,这会导致npm在之后阻塞,因为bin目录丢失。也许在linux上,它会得到适当的恢复,但不是windows。

#1


124  

If using Bash, just switch into the folder that has your package.json file and run the following:

如果使用Bash,请切换到包含包的文件夹。json文件并运行如下:

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

In the case of globally-installed packages, switch into your %appdata%/npm folder (if on Windows) and run the same command.

在全局安装包的情况下,切换到%appdata%/npm文件夹(如果在Windows上)并运行相同的命令。

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 3.3.6(节点5.0)时中断。我现在正在使用下面的Bash命令,我已经将它映射到.bashrc文件中的npm_uninstall_all:

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

Added bonus? it's way faster!

额外的好处?这是更快!

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

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

#2


42  

This worked for me:

这工作对我来说:

command prompt or gitbash into the node_modules folder in your project then execute:

命令提示符或gitbash进入项目中的node_modules文件夹,然后执行:

npm uninstall *

Removed all of the local packages for that project.

删除该项目的所有本地包。

#3


27  

For windows go to node_modules dir and run this in powershell

对于windows,转到node_modules目录并在powershell中运行它

npm uninstall (Get-ChildItem).Name 

#4


11  

I recently found a node command that allows uninstalling all the development dependencies as follows:

我最近发现了一个节点命令,它允许卸载所有开发依赖项,如下所示:

npm prune --production

As I mention, this command only uninstalls the development dependency packages. At least it helped me not to have to do it manually.

如前所述,此命令只卸载开发依赖包。至少它帮助我不必手动操作。

#5


8  

Actually there is no option to do that, if you want to uninstall packages from package.json simply do npm ls on the same directory that package.json relies and use npm uninstall <name> or npm rm <name> for the package you want to remove.

实际上,如果您想要从包中卸载包,就没有这个选项。json仅在包所在的目录上执行npm ls。json依赖并使用npm卸载 <名称> 或npm rm <名称> ,用于您想要删除的包。

#6


4  

Tip for Windows users: Run this PowerShell command from within node_modules parent directory:

Windows用户提示:在node_modules父目录中运行这个PowerShell命令:

ls .\node_modules | % {npm uninstall $_}

#7


1  

// forcibly remove and reinstall all package dependencies
ren package.json package.json-bak
echo {} > package.json
npm prune
del package.json
ren package.json-bak package.json
npm i

This essentially creates a fake, empty package.json, calls npm prune to remove everything in node_modules, restores the original package.json and re-installs everything.

这实际上创建了一个虚假的空包。json,调用npm prune删除node_modules中的所有内容,重新存储原始包。json和重新安装。

Some of the other solutions might be more elegant, but I suspect this is faster and exhaustive. On other threads I've seen people suggest just deleting the node_modules directory, but at least for windows, this causes npm to choke afterward because the bin directory goes missing. Maybe on linux it gets restored properly, but not windows.

其他的一些解决方案可能更优雅,但我怀疑这是更快和详尽的。在我看到的其他线程中,人们建议删除node_modules目录,但至少对于windows来说,这会导致npm在之后阻塞,因为bin目录丢失。也许在linux上,它会得到适当的恢复,但不是windows。