How can I uninstall npm modules in Node.js?

Better Stack Team
Updated on March 11, 2024

To uninstall npm modules (packages) in Node.js, you can use the npm uninstall command followed by the name of the module you want to remove. Here are a few examples:

Uninstalling a Local Module:

To remove a module from your project's node_modules directory and update your package.json:

 
npm uninstall <module_name>

Replace <module_name> with the name of the module you want to uninstall.

Uninstalling a Global Module:

To remove a globally installed module:

 
npm uninstall -g <module_name>

Uninstalling Multiple Modules:

To uninstall multiple modules at once:

 
npm uninstall <module1> <module2> <module3>

Uninstalling and Saving to devDependencies:

If you want to uninstall a module and remove it from devDependencies in your package.json:

 
npm uninstall --save-dev <module_name>

Uninstalling and Saving to dependencies:

If you want to uninstall a module and remove it from dependencies in your package.json:

 
npm uninstall --save <module_name>

Uninstalling and Updating package.json:

In some cases, npm might not automatically remove the entry from your package.json file. If that happens, you can use the --save or --save-dev flag to ensure it is removed:

 
npm uninstall --save <module_name>

or

 
npm uninstall --save-dev <module_name>

After running any of these commands, npm will remove the specified module from your project or global installation.

Make sure to check your package.json file and verify that the entry for the uninstalled module has been removed. If it hasn't, you may need to remove it manually.