In Node.js, you can uninstall installed modules using npm (Node Package Manager). The basic command format for uninstalling npm modules is as follows:
bashnpm uninstall <module_name>
Here are the detailed steps and examples:
- Uninstalling Modules Globally:
If the module is globally installed, you need to use the -g flag to uninstall it. For example, to globally uninstall the module named nodemon, you can use the following command:
bashnpm uninstall -g nodemon
- Uninstalling Modules Locally:
If the module is installed as a project dependency, you can directly execute the uninstall command in the project root directory. For example, if your project uses express, the command to uninstall it is:
bashnpm uninstall express
This will remove the express module from your node_modules directory and update the dependency information in both package.json and package-lock.json files.
- Uninstalling Modules from Dependency Lists:
If you used the --save, --save-dev, or --save-optional flags when installing the module, you should consider whether to remove it from the relevant dependency lists during uninstallation. For example, if express was installed as a development dependency (dev dependency), the command to uninstall and update package.json is:
bashnpm uninstall --save-dev express
- Verifying Correct Uninstallation:
After uninstallation, you can verify if the module has been correctly uninstalled by checking the project's node_modules directory or using the npm list command.
bashnpm list npm list -g # for checking global modules
Note that sometimes, even after uninstalling a module, its dependencies may still remain in the node_modules directory. To thoroughly clean up unnecessary modules, you can use the npm prune command to remove all modules not specified in the package.json file from your project.
This covers the basic steps for uninstalling npm modules in Node.js.