如何在Node JS中卸载NPM模块?

node.js Node.js

Harry乐Gil

2020-03-13

正如公知的,任何NPM模块可以通过运行一个简单的命令被安装:npm install <module_name>

我已经安装了一些不再使用的模块,我只想把它们取下来。我对此有一些疑问:

  • 我们是否有任何命令或过程可以从根目录卸载模块(例如npm uninstall <module_name>),还是仅删除模块文件即可?

  • 如果我们保留未使用的模块,它将对我们有何影响?

第1500篇《如何在Node JS中卸载NPM模块?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

21个回答
null 2020.03.13

To remove the module from your package.json run this command: npm unistall --save

To uninstall a module without changing package.json file run the following command: npm uninstall

To uninstall global package simply run this command: npm unistall -g

凯Gil 2020.03.13

If to want to uninstall a number of module the just run the npm uninstall. Then go to package.json and delete the unwanted module from there, and then just run the command npm install . It should fix your problem.

西门达蒙 2020.03.13

To uninstall the node module:

npm uninstall <module_name>  

This will remove the module from node_modules, but not from package.json.

Remove the module from package.json use by using this command:

npm uninstall <module_name> --save 

This also delete from package.json.

十三西里古一 2020.03.13

The command for uninstalling node module:

npm uninstall <module_name>

This will uninstall module from your local node-module directory, this will not affect application.

But if you are refer to global scope or want to change dependencies in package.json

here are some different options

npm uninstall <module_name> --save to remove module from dependencies in package.json.

npm uninstall <module_name> --save-dev to remove module from devDependencies in package.json.

npm uninstall <module_name> -g --save to remove module globally.

Full documentation with all option, refer npm uninstall

神乐泡芙 2020.03.13

要使用npm卸载模块,可以使用:

npm uninstall moduleName

另外,如果您想卸载并将更改反映在package.json中,则可以使用--save标志,如下所示:

npm uninstall moduleName --save
OR
npm uninstall -S

And if you want to uninstall a module from devDependencies and want the change to be reflected in package.json then you can use -D flag, like this:

npm uninstall moduleName -D
米亚神无 2020.03.13

In case you are windows run CMD as administrator and type npm -g uninstall <package name> .

番长神乐小小 2020.03.13

Use

npm uninstall <package_name>

Example to uninstall express

npm uninstall express
Mandy逆天 2020.03.13

您可以通过以下方式卸载节点模块

  1. 卸载软件包

    npm uninstall <Package Name>

  2. 卸载软件包并将其从package.json中的依赖项中删除

    npm uninstall <Package Name> --save

  3. 卸载软件包并将其从package.json中的dev依赖项中删除

    npm uninstall <Package Name> --save-dev

  4. 卸载全局软件包。全局软件包可以在系统的任何地方访问,而不仅限于特定项目

    npm uninstall -g <Package Name>

卡卡西逆天 2020.03.13

最简单的解决方案是:

npm uninstall packageName --save-dev

查看项目中的上层软件包名称:

npm list --depth=0

输出将如下所示:

app@0.1.0 /home/jackkobec/projects/myAppName
├── packageName@packageVersion
├── express@4.16.4

复制软件包名称并执行npm uninstall命令。快递包裹示例:

npm uninstall express --save-dev
Near逆天 2020.03.13

您还可以将以下内容用作速记:

npm un packageName 要么 npm rm packageName

注意:-g在命令末尾添加以卸载全局软件包。

YOC41811590 2020.03.13

有时npm uninstall -g packageName不起作用。

在这种情况下,您可以手动删除软件包。

在Mac上,转到文件夹/usr/local/lib/node_modules并删除包含所需软件包的文件夹。而已。使用此命令检查全局安装的软件包列表npm list -g --depth=0

阿飞泡芙 2020.03.13

uninstall当我尝试使用与安装时使用的命令相同的命令时,选项对我不起作用(因为我正在使用@latest指令进行安装

因此,例如,我安装了这样的软件包:

npm install  @ngtools/webpack@latest

然后我想卸载它,所以我使用了相同的命令(包括@latest)

npm uninstall  @ngtools/webpack@latest

所以上述卸载没有用,我必须删除@latest&然后运行良好

npm uninstall  @ngtools/webpack

我希望这有帮助

樱十三 2020.03.13

更新npm 5:

npm 5.0.0开始,默认情况下已添加/删除了已安装/未安装的模块作为依赖项,因此不再需要--save选项。

npm uninstall <package>

例如:

npm uninstall mongodb

它将从node_modules文件夹和package.json文件中删除该模块

Harry乐Gil 2020.03.13

对于Windows用户-如果要一次删除所有安装的节点模块:

  • 打开Powershell
  • 进入node_modules文件夹(cd node_modules)
  • 运行此命令-“ npm卸载(Get-ChildItem).Name”

它将卸载所有模块。

A神无 2020.03.13

即使看起来很明显,我也很难做到这一点

我最初尝试通过npm uninstall module-name脚本中的简单for循环遍历运行的node_modules目录我发现如果您调用完整路径,它将无法正常工作,例如

npm uninstall module-name

在工作,但是

npm uninstall /full/path/to/node_modules/module-name 

没有工作。

阳光西里小哥 2020.03.13

我只是默认在我的主目录下安装手写笔,所以我只是npm uninstall stylus用来分离它,或者您可以尝试npm rm <package_name>一下。

神乐Near 2020.03.13

node_modules/批量删除软件包,也可以从中删除它们package.json,进行保存,然后npm prune在终端上运行

这将删除文件系统中存在但未使用/未声明的那些软件包package.json

PS>这在Windows上特别有用,因为由于“超出路径长度限制”,您可能经常遇到无法删除某些文件的问题。

神无猪猪 2020.03.13

如果无法使用npm uninstall <module_name>,请通过键入全局尝试-g

也许您只需要以超级用户/管理员的身份进行操作即可sudo npm uninstall <module_name>

蛋蛋Itachi 2020.03.13

为了完全回答这个问题,有两种方法:(例如,我们将已安装的模块称为module1)。

  1. 要在更改package.json的情况下删除module1

    npm uninstall module1

  2. 通过更改package.json 来删除module1 ,并将其从package.json中的依赖项中删除,请执行以下操作:

    npm uninstall --save module1

注意:为简化上述命令,您可以使用-S代替--save,并且可以使用removermrununlink而不是uninstall

null 2020.03.13

卸载节点模块:

npm uninstall <module_name>  

这将从node_modules中删除该模块,但不会从package.json中删除该模块。因此,当我们再次执行npm install时,它将下载该模块。

因此,要从package.json中删除模块,请使用:

npm uninstall <module_name> --save  

这也会从package.json中删除依赖项。

如果要卸载任何全局模块,可以使用:

npm -g uninstall <module_name> --save 

这将全局删除依赖项。

Pro路易 2020.03.13

该命令很简单 npm uninstall <name>

Node.js文档https://npmjs.org/doc/具有npm所需的所有命令。

本地安装将在node_modules/您的应用程序目录中。如果模块保留在其中而没有引用,则这不会影响应用程序。

但是,如果要删除全局软件包,则引用该全局软件包的所有应用程序都将崩溃。

以下是不同的选项:

npm uninstall <name> removes the module from node_modules but does not update package.json

npm uninstall <name> --save also removes it from dependenciesin package.json

npm uninstall <name> --save-dev also removes it from devDependencies in package.json

npm -g uninstall <name> --save also removes it globally

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android