Digital Marketing

How to properly uninstall npm and its packages?

By Steven, on December 5, 2024 - 3 min read

Node Package Manager (npm) is the go-to tool for managing dependencies in a Node.js project. But what should you do when you want to get rid of npm or one of its packages that has become useless?

Debugbar has prepared a complete guide to properly uninstall npm and associated packages, whether they are local dependencies for a project or globally installed packages.

Completely removing npm and Node.js

You want to completely remove Node.js and npm from your device? Here is how to do it depending on the system you are working on (ubuntu, Windows, Mac).

Uninstalling Node.js and npm on Windows

The best way to uninstall node.js on Windows is to use the control panel:

  1. Open the control panel.
  2. Click on Programs > Programs and Applications (or Settings > Apps for Windows 11).
  3. In the list of programs, locate Node.js.
  1. Right click on it and choose “Uninstall“.
  2. Follow the instructions and restart your PC.

You can also go through the command prompt (search for “cmd”) by running msiexec /x {node-version-number}. You will find the version number installed in the C:\Program Files (x86)\nodejs directory or C:\Program Files\nodejs.

Uninstalling Node.js on macOS

To uninstall Node.js on your Mac you will need to:

  • open the Terminal and enter the command:
  • sudo rm -rf /usr/local/{bin/{node,npm},lib/node_modules/npm,lib/node,share/man/*/node.*}

This will remove Node.js, npm and associated folders.

You can also choose the classic method:

  1. Press Cmd + Shift + G simultaneously to open the execution tool.
  2. Type /usr/local/lib
  1. Find the node_modules folder, right click and click “Move to Trash“.

Finally restart your Mac and you’re done.

Uninstalling Node.js on Linux (Ubuntu / Debian)

If you are on Linux / Ubuntu, you need to:

  1. Open a terminal and run: sudo apt-get remove nodejs npm
  2. Clean up unused packages with: sudo apt autoremove

You don’t want to completely uninstall npm? Then here’s how to uninstall only the packages.

Uninstalling a local npm package from a Node.js project

The easiest way to remove an npm package from a project is to use the npm uninstall command.

To do this, simply go to the root of the project (where the package.json file is located) and type: npm uninstall <package-name>

For example, if you want to uninstall the lodash package, you will need to type: npm uninstall lodash

This will remove lodash from the node_modules folder and update package.json and package-lock.json accordingly. If the package was part of dependencies, it will be removed from that section.

Uninstalling a package without touching the package.json

If you want to uninstall a package without modifying the package.json, you just need to add the –no-save option:

  • npm uninstall –no-save <package-name>

The package will be removed from node_modules but will remain listed in the dependencies of package.json.

Checking that a package has been uninstalled

To make sure npm uninstall has done its job, you can inspect the contents of the node_modules folder:

  • On Unix/macOS, you will need to enter the following command: ls node_modules
  • On Windows you will need to run this command: dir node_modules

You should then, if all went well, see that the package folder has disappeared.

Uninstalling a development dependency (dev Dependency)

devDependencies are packages only used during development.

To properly uninstall them you have to add the –save-dev flag (or -D for short):

  • npm uninstall –save-dev <package-name>

Or:

  • npm uninstall -D <package-name>

This will remove the development dependency from the devDependencies section of the package.json in addition to deleting it from the node_modules folder.

Uninstalling a global npm package

Packages installed globally with -g are not tied to a project but depend on the system. To uninstall one, you will need to use npm uninstall with the -g option:

  • npm uninstall -g <package-name>

The global package will then be removed.

In short: the npm uninstall command is your best friend for cleanly removing local and global packages that have become unnecessary. Use it with the right options according to your needs. And if you want to start with a clean Node.js environment, follow the complete uninstall steps for your OS. Do some tidying up in your node_modules, your hard drive will thank you!

Steven