乐闻世界logo
搜索文章和话题

How can I uninstall npm modules in NodeJS ?

5 个月前提问
3 个月前修改
浏览次数78

6个答案

1
2
3
4
5
6

在Node.js中,可以通过npm(Node包管理器)来卸载已安装的模块。卸载npm模块的基本命令格式如下:

bash
npm uninstall <module_name>

这里是详细步骤和示例:

  1. 全局卸载模块: 如果模块是全局安装的,需要使用 -g标志来卸载它。例如,如果您想全局卸载名为 nodemon的模块,您可以使用以下命令:

    bash
    npm uninstall -g nodemon
  2. 本地卸载模块: 如果模块是作为项目依赖安装的,那么您可以直接在项目根目录中执行卸载命令。比如您的项目使用了 express,卸载它的命令如下:

    bash
    npm uninstall express

这将会从您的 node_modules目录中移除 express模块,并且同时更新 package.jsonpackage-lock.json文件中的依赖信息。

  1. 保存到依赖列表中的卸载: 若您在安装模块时使用了 --save--save-dev--save-optional标志,卸载时也应该考虑是否需要从相关的依赖列表中移除。例如,如果 express是作为开发依赖(devDependency)安装的,卸载并更新 package.json的命令如下:

    bash
    npm uninstall --save-dev express
  2. 检查是否正确卸载: 卸载后,您可以通过检查项目的 node_modules目录,或者使用 npm list命令来确认模块是否已被正确卸载。

    bash
    npm list npm list -g # 用于检查全局模块

请注意,有时候即使卸载了一个模块,该模块的依赖可能仍然存在于 node_modules目录中。如果您想彻底清理不再需要的模块,可以使用如 npm prune的命令来移除项目中未列在 package.json文件中的所有模块。

以上就是在Node.js中卸载npm模块的基本步骤。

2024年6月29日 12:07 回复

The command is simply npm uninstall <name>

The Node.js documents https://npmjs.org/doc/ have all the commands that you need to know with 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.

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_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 uninstall -g <name> --save also removes it globally

2024年6月29日 12:07 回复

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

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

2024年6月29日 12:07 回复

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

  1. To remove module1 without changing package.json:

    npm uninstall module1

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

    npm uninstall --save module1

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

2024年6月29日 12:07 回复

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

2024年6月29日 12:07 回复

To uninstall the Node.js module:

shell
npm uninstall <module_name>

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

So to remove the module from file package.json, use:

shell
npm uninstall <module_name> --save

This also deletes the dependency from file package.json.

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

shell
npm -g uninstall <module_name> --save

This will delete the dependency globally.

2024年6月29日 12:07 回复

你的答案