How to Remove an NPM Package: npm uninstall Guide

How to Remove an NPM Package: npm uninstall Guide

Have you ever encountered a situation where you don’t need some NPM packages anymore in your project and want to remove them? I’m sure you did.

A straightforward way to achieve it that I’m sure every one of us has done once in our development journey is to remove the entry of the NPM packages you want to uninstall from the package.json file, delete the node_modules folder, and re-install all the dependencies and dev dependencies using npm install.

There are two significant problems with the above approach,

  1. Re-installing node_modules every time you need to remove an NPM package doesn’t seem to be a good idea. Often node_modules are pretty heavy and require some time to install.
  2. The above approach may work fine for local NPM packages but won’t work with global NPM packages. The reason is that there is no global package.json file in this case. NPM doesn’t keep track of global packages. They are simply installed in a global node_modules folder.

Quick Tip
You can find out where your global NPM packages are installed by running the command: npm root -g.

So, what’s the solution?

The solution is to use the npm uninstall command provided by the NPM package manager. To uninstall a package from your project, we can use the following command,

npm uninstall <package_name>Code language: Bash (bash)

Consider a simple express application,

It uses the following packages:

  1. Express.js (Dependency)
  2. Chalk (Dependency)
  3. Nodemon (Dev dependency)

Suppose we don’t need the chalk package anymore, so we’ll use the following command,

npm uninstall chalkCode language: Bash (bash)

Using the above command will remove the chalk package and update the package.json and package-lock.json files,

Quick Tip
We can also use the npm un command instead of npm uninstall.

Removing Dev Dependencies

As we already know, a dev dependency is a package used only during development. While installing a package as a dev dependency, we provide a --save-dev or -D flag. The same goes while uninstalling a dev dependency. To uninstall a dev dependency, we’ll use the following command,

npm uninstall --save-dev <package_name>
# or
npm uninstall -D <package_name>Code language: Bash (bash)

Let’s suppose we want to uninstall the nodemon package. Now, since it’s a dev dependency, we’ll use the command,

npm uninstall --save-dev nodemon
# or
npm uninstall -D nodemonCode language: Bash (bash)

The above command will remove the nodemon package and update the package.json and package-lock.json files. Since nodemon was the only dev dependency, the whole devDependency key has been removed from the package.json file,

Removing Global NPM packages

Unlike local packages that are only available on local projects, global NPM packages are installed globally and are available throughout your system. While installing an NPM package globally, we use the -g flag. Similarly, while uninstalling a global NPM package, we’ll provide the -g flag,

npm uninstall -g <package_name>Code language: Bash (bash)

But how do we see the global packages installed on our system?

To see the global packages installed on our system we can use the following command,

npm list -gCode language: Bash (bash)

This is will show us the list of global packages installed on our system,

Now, let’s remove the fcommit package. Since it’s a global package, we’ll use the following command,

npm uninstall -g fcommitCode language: Bash (bash)

This will remove the fcommit package globally from our system. We can also verify it using the npm list -g command again,

Conclusion

In this article, We learned the various ways in which we can uninstall different kinds of NPM packages, so we can reduce the size of our node_modules by removing remove unnecessary packages.

Thank you so much for reading ?

Sharing is caring

Did you like what Varun Tiwari wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far

Curious about this topic? Continue your journey with these coding courses: