Back

How to remove global and local packages with npm

How to remove global and local packages with npm

Unused npm packages take up disk space and can cause compatibility with other packages. Periodically removing packages that you no longer need is vital to keeping your project clean. When it comes to uninstalling dependencies, the right tool to use is npm uninstall. This guide walks through the process of using that npm utility to uninstall a package, both globally and locally.

npm uninstall: The Command to Remove npm Dependencies

npm uninstall is a command-line utility provided by npm to uninstall packages. You can use it with the following syntax:

npm uninstall <package_name>

Replace <package_name> with the name of the npm package you want to uninstall. Remember that the utility also supports optional arguments to customize its behavior.

npm uninstall deletes the package from the folder where it is stored. In the case of a local package, it also removes it from the dependencies, devDependencies, optionalDependencies, and peerDependencies objects in your package.json file. Furthermore, it updates package-lock.json.

Time to see how to use it to uninstall npm packages and dependencies!

How To Uninstall Global Packages

Use the npm uninstall command to remove a global npm package as below:

npm uninstall -g <package_name>

Or, equivalently, for scoped packages:

npm uninstall -g <@scope/package_name>

Note: -g is an abbreviation for the --global option.

For example, remove lodash globally with:

npm uninstall -g lodash

The output will be something like:

removed 1 package in 814ms

As a result, the lodash package has been removed from the folder where Node.js is installed.

How To Uninstall Local Packages

Launch the command below in your npm project folder to uninstall a package:

npm uninstall <package_name>

Equivalently, in the case of a scoped package, run:

npm uninstall <@scope/package_name>

By default, npm uninstall removes the package from your package.json, npm-shrinkwrap.json, and package-lock.json files. You can avoid that by adding the --no-save option:

npm uninstall --no-save <package_name>

For example, you can remove axios from your project with:

npm uninstall axios

The output will be something close to the following:

removed 1 package, and audited 1 package in 803ms

The axios folder in node_modules will be deleted, and the package will be removed from package.json and package-lock.json.

FAQs

Is it possible to remove multiple packages with the same npm uninstall command?

Yes, you can remove multiple packages at once by listing them all, separated by spaces:

npm uninstall <package_name_1> <package_name_2> ... <package_name_n>

How can you verify that a local package was removed by uninstalling npm?

Check that the node_modules directory no longer contains a directory for the uninstalled package. Also, make sure that package.json no longer contains the package in the dependencies, devDependencies, optionalDependencies, or peerDependencies objects.

How do you remove local packages without uninstalling npm?

Manually remove the package from the dependencies, devDependencies, optionalDependencies, or peerDependencies object in package.json and then run npm install.

Conclusion

Removing unused or outdated npm packages makes your projects cleaner. By following our instructions, you learned how to delete local and global packages with npm uninstall.

Gain control over your UX

See how users are using your site as if you were sitting next to them, learn and iterate faster with OpenReplay. — the open-source session replay tool for developers. Self-host it in minutes, and have complete control over your customer data. Check our GitHub repo and join the thousands of developers in our community.

OpenReplay