How can I uninstall npm modules in Node.js?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Node.js is a powerful platform for building various types of applications, and npm (Node Package Manager) plays a critical role as the default package manager for the Node.js JavaScript runtime environment. It helps in managing packages required by an application, but there are situations where you may need to uninstall some of these npm modules. This might be necessary to free up space, resolve dependency issues, or simply because the module is no longer needed. Here’s how you can effectively uninstall npm modules in Node.js.
Understanding npm Uninstallation
Npm allows you to uninstall modules that you previously installed locally (in your project) or globally (across all projects). Uninstalling npm modules can be done using simple commands in your terminal or command prompt.
Uninstalling Local npm Modules
Local modules are those installed in the node_modules directory in your project. They are specific to the project and are not accessible by other projects. To uninstall a local npm module, navigate to your project directory in the terminal and run:
For example, if you want to uninstall lodash from your project, you would run:
This command removes the package from the node_modules directory and also updates the project’s package.json and package-lock.json files.
Uninstalling Global npm Modules
Global modules are installed system-wide and can be used by multiple projects. To uninstall a global npm module, you need to add the -g or --global flag to the uninstall command:
For instance, to uninstall the create-react-app package globally, the command would be:
Using the --save Flags
When uninstalling npm modules, you might notice references to the --save, --save-dev, or --save-optional flags. These flags are generally used for installation to indicate the nature of dependency (development, production, optional). However, starting from npm version 5, these flags are no longer needed for uninstallation as npm automatically updates the package.json and package-lock.json.
Checking Uninstalled Modules
After uninstalling modules, you can ensure that a module is removed by checking your package.json file or by listing all installed packages:
For global packages:
This command will show you the top-level packages currently installed.
Additional Cleanup
Sometimes, you might want to clean up the npm cache after uninstalling packages. Although this is generally not necessary, it can help resolve any cache-related issues.
Be cautious with this command as it forces a cache cleanup which might lead to unexpected issues.
Common Errors and Their Solutions
- Uninstalling dependencies of dependencies: If you uninstall a module that other modules depend on, those modules might stop working. Ensure no other module requires the uninstalled package.
- Permission issues: When uninstalling global modules, permission issues might occur. Running the command prompt or terminal as an administrator or using
sudocan resolve this.
Summary Table
| Command | Description |
npm uninstall <name> | Removes a local package |
npm uninstall -g <name> | Removes a global package |
npm list --depth 0 | Lists top-level local packages |
npm list -g --depth 0 | Lists top-level global packages |
npm cache clean --force | Forces a cleanup of npm's cache |
By following these steps and using the commands provided, you can effectively manage and uninstall npm modules in your Node.js projects, ensuring they stay lean and maintainable.

