node.js 如何在节点 js 中卸载 npm 模块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13066532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:32:19  来源:igfitidea点击:

How to uninstall npm modules in node js?

node.jsnpm

提问by Manish Kumar

As commonly known, any npm module can be installed by running a simple command: npm install <module_name>.

众所周知,任何 npm 模块都可以通过运行一个简单的命令来安装:npm install <module_name>.

I have installed a few modules that I do not use anymore and I just want to get them off. I have a few questions regarding this:

我已经安装了一些我不再使用的模块,我只想把它们拿掉。我对此有几个问题:

  • Do we have any command or process to uninstall a module from the root (something like npm uninstall <module_name>) or will simply removing the module files do?

  • How does it affect us if we keep the unused modules?

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

  • 如果我们保留未使用的模块,对我们有什么影响?

回答by Menztrual

The command is simply npm uninstall <name>

命令很简单 npm uninstall <name>

The Node.js documents https://npmjs.org/doc/have all the commands that you need to know with npm.

Node.js 文档https://npmjs.org/doc/包含您需要使用 npm 了解的所有命令。

A local install will be in the node_modules/directory of your application. This won't affect the application if a module remains there with no references to it.

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

If you're removing a global package, however, any applications referencing it will crash.

但是,如果您要删除一个全局包,任何引用它的应用程序都会崩溃。

Here are different options:

以下是不同的选项:

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

npm uninstall <name>从中删除模块node_modules但不更新package.json

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

npm uninstall <name> --save也将其从dependenciesin 中删除package.json

npm uninstall <name> --save-devalso removes it from devDependenciesin package.json

npm uninstall <name> --save-dev也将其从devDependenciesin 中删除package.json

npm uninstall -g <name> --savealso removes it globally

npm uninstall -g <name> --save也会在全球范围内删除它

回答by fuma

If it doesn't work with npm uninstall <module_name>try it globally by typing -g.

如果它不起作用npm uninstall <module_name>,请通过键入全局尝试-g

Maybe you just need to do it as an superUser/administrator with sudo npm uninstall <module_name>.

也许您只需要以超级用户/管理员身份使用sudo npm uninstall <module_name>.

回答by Ehsan

Well to give a complete answer to this question, there are two methods: (for example we call the installed module as module1)

好吧,给这个问题一个完整的答案,有两种方法:(例如我们将安装的模块称为module1)

  1. To remove module1 withoutchanging package.json:

    npm uninstall module1

  2. To remove module1 withchanging package.json, and removing it from the dependencies in package.json:

    npm uninstall --save module1

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

    npm uninstall module1

  2. 通过更改 package.json删除 module1 ,并将其从 package.json 中的依赖项中删除:

    npm uninstall --save module1

Note: to simplify the above mentioned commands, you can use -Sinstead of --save, and can use remove, rm, r, un, unlinkinstead of uninstall

注意:为了简化上述命令,您可以使用-S代替--save,并且可以使用removermrununlink代替卸载

回答by GrahamLe

I just install stylusby default under my home dir, so I just use npm uninstall stylusto detach it, or you can try npm rm <package_name>out.

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

回答by Vishnu Mishra

To uninstall the node module:

卸载节点模块:

npm uninstall <module_name>  

This will remove the module from node_modules, but not from package.json. So when we do npm install again it will download the module.

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

So to remove the module from package.json use:

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

npm uninstall <module_name> --save  

This also delete the dependency from package.json.

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

And if you want to uninstall any globally module you can use:

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

npm -g uninstall <module_name> --save 

This will delete the dependency globally.

这将全局删除依赖项。

回答by last-indigo

To remove packages in node_modules/in bulk, you could also remove them from package.json, save it, and then run npm pruneon the terminal.

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

This will remove those packages, which exist in the filesystem, but are not used/declared package.json.

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

P.S> This is particularly useful on Windows, as you may often encounter problems with being unable to delete some files due to the "exceeded path length limit".

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

回答by kayleeFrye_onDeck

I found this out the hard way, even if it is seeminglyobvious.

我发现这一点很困难,即使它看起来很明显。

I initially tried to loop through the node_modules directory running npm uninstall module-namewith a simple for loop in a script. I found out it will not work if you call the full path, e.g

我最初尝试npm uninstall module-name在脚本中使用简单的 for 循环来遍历 node_modules 目录。我发现如果你调用完整路径它不会工作,例如

npm uninstall module-name

was working, but

正在工作,但是

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

was not working.

没有工作。

回答by Mwiza

You can also run the following as shorthand:

您还可以将以下内容作为速记运行:

npm un packageNameor npm rm packageName

npm un packageName或者 npm rm packageName

Note: Add -gat end of command to uninstall global packages.

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

回答by Nastro

Sometimes npm uninstall -g packageNamedoens't work.

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

In this case you can delete package manually.

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

On Mac go to folder /usr/local/lib/node_modulesand delete folder with package you want. That's it. Check your list of globally installed packages with this command npm list -g --depth=0

在 Mac 上,转到文件夹/usr/local/lib/node_modules并删除包含您想要的包的文件夹。就是这样。使用此命令检查全局安装的软件包列表npm list -g --depth=0

回答by codemirror

Update npm 5:

更新 npm 5:

As of npm 5.0.0, installed/uninstalled modules are added/removed as a dependency by default, so the --save option is no longer needed.

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

run

npm uninstall <package>

for example:

例如:

npm uninstall mongodb

It will remove the module from node_modules folder and package.json file also

它也会从 node_modules 文件夹和 package.json 文件中删除模块