How to uninstall npm package

How to uninstall npm package

npm-uninstall

Table of contents

This uninstalls a package, completely removing everything npm installed on its behalf.

Save installed packages to a package.json file as dependencies.

Enable running a command in the context of the configured workspaces of the current project while filtering by running only the workspaces defined by this configuration option.

Valid values for the workspace config are either:

When set for the npm init command, this may be set to the folder of a workspace which does not yet exist, to create the folder and set it up as a brand new workspace within the project.

This value is not exported to the environment for child processes.

Set to true to run the command in the context of all configured workspaces.

Explicitly setting this to false will cause commands like install to ignore workspaces altogether. When not set explicitly:

This value is not exported to the environment for child processes.

Include the workspace root when workspaces are enabled for a command.

When false, specifying individual workspaces via the workspace config, or all workspaces via the workspaces flag, will cause npm to operate only on the specified workspaces, and not on the root project.

This value is not exported to the environment for child processes.

When set file: protocol dependencies that exist outside of the project root will be packed and installed as regular dependencies instead of creating a symlink. This option has no effect on workspaces.

npm Uninstall – How to Remove a Package

How to uninstall npm package. Смотреть фото How to uninstall npm package. Смотреть картинку How to uninstall npm package. Картинка про How to uninstall npm package. Фото How to uninstall npm package

The Node Package Manager (NPM) provides various commands that let you work with packages.

And just as you can install a package from the npm library, you can uninstall it.

The way you uninstall a regular package or dependency is not the way you should uninstall a global package and a dev dependency, though.

In this article, I will show you how to uninstall a regular package, a global package, and a dev dependency.

How to Remove a Package with npm Uninstall

To remove a package with the npm uninstall command, you can use the syntax npm uninstall package-name in the directory where the package is located.

The package I will be using to demonstrate how a package is uninstalled is Express – a NodeJS framework.

In the screenshot below, you can see that Express is listed as a dependency in the package.json file.
How to uninstall npm package. Смотреть фото How to uninstall npm package. Смотреть картинку How to uninstall npm package. Картинка про How to uninstall npm package. Фото How to uninstall npm package

How to uninstall npm package. Смотреть фото How to uninstall npm package. Смотреть картинку How to uninstall npm package. Картинка про How to uninstall npm package. Фото How to uninstall npm package

You can see there’s no Express anymore. There’s even no dependency key anymore because there is no dependency.

How to Remove a Dev Dependency with npm Uninstall

A dev dependency is a package used during development only.

You must run the command in the directory (folder) where the dependency is located.

I will be using Nodemon to demonstrate how to remove a dev dependency.

Nodemon lets your NodeJS app reload automatically any time it detects a change in a file or folder during development.

In the screenshot below, you can see that Nodemon is listed as a dev dependency.
How to uninstall npm package. Смотреть фото How to uninstall npm package. Смотреть картинку How to uninstall npm package. Картинка про How to uninstall npm package. Фото How to uninstall npm package

To remove it, I will run npm uninstall –D nodemon
How to uninstall npm package. Смотреть фото How to uninstall npm package. Смотреть картинку How to uninstall npm package. Картинка про How to uninstall npm package. Фото How to uninstall npm package

You can see there’s no Nodemon anymore in the package.json file.

How to Remove a Global Package with npm Uninstall

A global package is a package that is installed globally on your machine, so you don’t have to reinstall it every you need it.

To show you how to remove a global package, I will be using a package called CORS (Cross-origin Resource Sharing).

CORS blocks the Same Origin Policy (SOP) of browsers so you can make requests from one browser to another.

In the screenshot below, you can see that CORS is not listed as a package in the package.json file:
How to uninstall npm package. Смотреть фото How to uninstall npm package. Смотреть картинку How to uninstall npm package. Картинка про How to uninstall npm package. Фото How to uninstall npm package

CORS is not listed because it is installed globally on my machine, not in the directory of a project.

You can see that CORS is listed as a global package now.

After running the command, you can see there’s no CORS anymore when I run npm list –g :

How to uninstall npm package. Смотреть фото How to uninstall npm package. Смотреть картинку How to uninstall npm package. Картинка про How to uninstall npm package. Фото How to uninstall npm package

Conclusion

In this article, you learned the various ways you can uninstall different kinds of NPM packages, so you can have more control over your codebase and remove unnecessary packages.

Thank you for reading.

If you find this article helpful, kindly share it so others can see it.

Web developer and technical writer focusing on frontend technologies.

If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started

freeCodeCamp is a donor-supported tax-exempt 501(c)(3) nonprofit organization (United States Federal Tax Identification Number: 82-0779546)

Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.

How to uninstall npm package?

I’ve installed grunt using sudo npm install grunt and now I can’t remove it.

But it gives me a WARN :

But I still can run grunt from command line.

EDIT:

EDITOR NOTE:

This question was asked over 5 years ago as How to uninstall npm package. It’s been a very useful question favourited by many, who found a solution to their problem, so I’m changing it back from a recent edit that called it How to uninstall grunt package, because it requires the same procedure as any other npm package.

6 Answers 6

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

To uninstall a npm module from project node_modules folder, run:

One can also remove a local dependency/module installation, by deleting its directory from the local node_modules folder. Yes, it’s safe to delete dependencies there.

To uninstall a npm module that was installed globally, run:

It doesn’t matter where you run this command from.

To install a npm module, run: (only meant as reference)

npm install (if there’s a package.json file at the root of your project)

Good things to know about Grunt:

grunt and grunt-cli are two different things.

grunt is only installed locally (by running npm install grunt ).

On npm and sudo

Quoting Isaac Z. Schlueter on his Introduction to npm article:

I strongly encourage you not to do package management with sudo! Packages can run arbitrary scripts, which makes sudoing a package manager command as safe as a chainsaw haircut. Sure, it’s fast and definitely going to cut through any obstacles, but you might actually want that obstacle to stay there.

I recommend doing this once instead:

That sets your user account as the owner of the /usr/local directory, so that you can just issue normal commands in there. Then you won’t ever have to use sudo when you install node or issue npm commands.

It’s much better this way. /usr/local is supposed to be the stuff you installed, after all.

Yet another catch mentioned by Andrei Karpushonak:

There are certain security concerns and functionality limitations regarding changing the ownership of /usr/local to the current user:

npm command to uninstall or prune unused packages in Node.js

How to uninstall npm package. Смотреть фото How to uninstall npm package. Смотреть картинку How to uninstall npm package. Картинка про How to uninstall npm package. Фото How to uninstall npm package

How to uninstall npm package. Смотреть фото How to uninstall npm package. Смотреть картинку How to uninstall npm package. Картинка про How to uninstall npm package. Фото How to uninstall npm package

3 Answers 3

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

From npm help prune :

This command removes «extraneous» packages. If a package name is provided, then only packages matching one of the supplied names are removed.

Extraneous packages are packages that are not listed on the parent package’s dependencies list.

You can use npm-prune to remove extraneous packages.

This command removes «extraneous» packages. If a package name is provided, then only packages matching one of the supplied names are removed.

Extraneous packages are packages that are not listed on the parent package’s dependencies list.

If the —production flag is specified or the NODE_ENV environment variable is set to production, this command will remove the packages specified in your devDependencies. Setting —no-production will negate NODE_ENV being set to production.

If the —dry-run flag is used then no changes will actually be made.

If the —json flag is used then the changes npm prune made (or would have made with —dry-run) are printed as a JSON object.

In normal operation with package-locks enabled, extraneous modules are pruned automatically when modules are installed and you’ll only need this command with the —production flag.

If you’ve disabled package-locks then extraneous modules will not be removed and it’s up to you to run npm prune from time-to-time to remove them.

Use npm-dedupe to reduce duplication

Searches the local package tree and attempts to simplify the overall structure by moving dependencies further up the tree, where they can be more effectively shared by multiple dependent packages.

For example, consider this dependency graph:

In this case, npm-dedupe will transform the tree to:

Because of the hierarchical nature of node’s module lookup, b and d will both get their dependency met by the single c package at the root level of the tree.

The deduplication algorithm walks the tree, moving each dependency as far up in the tree as possible, even if duplicates are not found. This will result in both a flat and deduplicated tree.

How to install/uninstall an npm package

Renat Galyamov in Code June 28, 2019 Write a comment

Learn the basics of installing and uninstalling npm packages, locally and globally.

How to install an npm package

First of all, let’s learn how to install an npm package/module. It can be done by running this simple command:

Shorthand for npm install

When you install a package locally it will install it to the local node_modules folder.

Hi, I’m Renat 👋

Install an npm package and save it as a dependency

When you install a package as a dependency it will add it to package.json file as a dependency.

Install an npm package and save it as a dev dependency

Installing a package globally

For example, if you want to install an npm package globally run this command:

Uninstalling npm packages

Similarly to installing npm packages they can be uninstalled (locally and globally).

To uninstall an npm package run the following command.

To uninstall a package globally

Uninstalling npm package from dependencies

Uninstalling npm package from devDependencies

Install packages using package.json

If your project contains a package.json file you can install all modules listed there by running this command:

Shorthand for npm install

It will install dependencies from the package.json file in the local node_modules folder.

If you find this post useful, please let me know in the comments below and subscribe to my newsletter.
Cheers,
Renat Galyamov

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *