How can I uninstall npm modules in Node.js?
Asked Answered
G

23

1413

As commonly known, any npm module can be installed by running a simple command: npm install <module_name>.

I have installed a few modules that I do not use any more and I just want to get them off. I have a few questions regarding this:

  • Do we have any command or process to uninstall a module from the root (something like npm uninstall <module_name>) or will simply removing the module files do?

  • How does it affect us if we keep the unused modules?

Geraud answered 25/10, 2012 at 10:23 Comment(1)
If you want to remove all npm modules, go here: #9283972Pending
S
2056

The command is simply npm uninstall <name>

The Node.js documents https://npmjs.org/doc/ have all the commands that you need to know with npm.

A local install will be in the node_modules/ directory of your application. This won't affect the application if a module remains there with no references to it.

If you're removing a global package, however, any applications referencing it will crash.

Here are different options:

npm uninstall <name> removes the module from node_modules but does not update package.json

npm uninstall <name> --save also removes it from dependenciesin package.json

npm uninstall <name> --save-dev also removes it from devDependencies in package.json

npm uninstall -g <name> --save also removes it globally

Schrimsher answered 25/10, 2012 at 10:33 Comment(14)
try this npm -g ls | grep -v 'npm@' | awk '/@/ {print $2}' | awk -F@ '{print $1}' | xargs npm -g rm to uninstall all gobally installed module. if you are on windows then you need to install Cygwin or git to run itTaliesin
@nmrony's command should work for Mac users too. I used it in Yosemite, but had to cd /usr/local/lib and then change the last statement to xargs npm uninstall.Lambert
For some reason I cannot use "npm uninstall ___" or even "sudo npm uninstall ___". I do not have privileges to remove the files. I can only remove npm packages by going to the directory and force removing everything by hand. (using rm in the terminal). How to fix this?Consistent
Global modules are stored in the node_modules directory in the node installation root directory. If you don't want to run a lengthy command, just delete the module directories from there. Don't just blindly nuke everything though.... npm is a module in that directory too!Cystocarp
For uninstalling all global modules: #5927172 And delete node_modules folder For uninstalling all local modules delete local node_modules folderOverliberal
Also you can use npm list -g --depth=0 to get the globally installed npm modules.Viol
@IshikawaYoshi, yes by going to the path: /usr/local/lib/ there you will find node_modules folder, by deleting it, it will clear everything.Flub
alias include: un, unlink, remove, rm, r npm r <module>Bilge
I have tried to remove [email protected] using all the commands listed above, & it is still thereSlattern
npm uninstall <name> now removes the module from both package.json` and node_modulesSorites
I tried to npm uninstall package@version too and it didn't work. It did work when I did npm uninstall package (without the @version) cc @SteveStaple (as noted in this answer https://mcmap.net/q/45295/-how-can-i-uninstall-npm-modules-in-node-js)Aymara
as of 2019, node 13 (I believe 10+ as well) removes packages from package.json via npm uninstall <name>Citation
As comments and an answer mention, the --save flag is no longer useful as it is the default behavior without the flag; if you want the old default behavior (leaving package.json untouched), use the --no-save flag. docs.npmjs.com/cli/installManipulate
I just test it without --save, and for sure package.json get updated. node -v v12.13.0 npm -v 6.13.0Stuyvesant
D
95

If it doesn't work with npm uninstall <module_name> try it globally by typing -g.

Maybe you just need to do it as an superUser/administrator with sudo npm uninstall <module_name>.

Debra answered 25/3, 2014 at 14:17 Comment(1)
-g is helpful if there is a npm WARN uninstall.Monacid
C
60

Well, to give a complete answer to this question, there are two methods (for example we call the installed module as module1):

  1. To remove module1 without changing package.json:

    npm uninstall module1

  2. To remove module1 with changing package.json, and removing it from the dependencies in package.json:

    npm uninstall --save module1

Note: to simplify the above mentioned commands, you can use -S instead of --save , and can use remove, rm, r, un, unlink instead of uninstall

Candlelight answered 16/2, 2016 at 15:10 Comment(2)
For the sake of completeness, one should be careful does he/she wanna remove the GLOBAL or LOCAL dependancy. For globals use -g with the above! Also: For uninstalling all global modules: #5927172 And delete node_modules folder For uninstalling all local modules delete local node_modules folderOverliberal
If anyone is silly like me you might also want to check that you're running npm uninstall ... from the correct directory as well!Liven
S
32

I just install stylus by default under my home dir, so I just use npm uninstall stylus to detach it, or you can try npm rm <package_name> out.

Slapbang answered 7/7, 2013 at 8:15 Comment(0)
J
30

To uninstall the Node.js module:

npm uninstall <module_name>

This will remove the module from folder node_modules, but not from file package.json. So when we do npm install again it will download the module.

So to remove the module from file package.json, use:

npm uninstall <module_name> --save

This also deletes the dependency from file package.json.

And if you want to uninstall any globally module you can use:

npm -g uninstall <module_name> --save

This will delete the dependency globally.

Jolyn answered 8/6, 2016 at 4:11 Comment(0)
V
20

To remove packages in folder node_modules in bulk, you could also remove them from file package.json, save it, and then run npm prune in the terminal.

This will remove those packages, which exist in the file system, but are not used/declared in file package.json.

P.S.: This is particularly useful on Windows, as you may often encounter problems with being unable to delete some files due to the "exceeded path length limit".

Vestavestal answered 11/10, 2017 at 12:21 Comment(0)
W
13

Sometimes npm uninstall -g packageName doesn’t work.

In this case you can delete package manually.

On Mac, go to folder /usr/local/lib/node_modules and delete the folder with the package you want. That's it. Check your list of globally installed packages with this command:

npm list -g --depth=0
Warnke answered 28/10, 2018 at 13:13 Comment(3)
Just curious, why doesn't "npm uninstall -g packageName" work in some cases ?Erythropoiesis
because it's huge buggy code... it really difficult to adapt to every OS version. I think that's is the reasonWarnke
It really saved my time! don't know why "npm uninstall -g packageName" do not work.Fatuous
C
9

Update for npm 5:

As of npm 5.0.0, installed/uninstalled modules are added/removed as a dependency by default, so the --save option is no longer needed.

Run

npm uninstall <package>

For example:

npm uninstall mongodb

It will remove the module from the node_modules folder and also the package.json file.

Castano answered 26/3, 2019 at 8:19 Comment(2)
True; if you want the old default behavior (leaving package.json untouched), use the --no-save flag docs.npmjs.com/cli/installManipulate
this answer and this comment should be way upKrp
P
9

Alias can be used to to uninstall node_modules package

un alias for uninstall

  • removes single package
 - npm un <PACKAGE_NAME> 
  • removes multiple packages by adding space between packages names
- npm un <PACKAGE_NAME_1> <PACKAGE_NAME_2> 
  • removes all node_modules packages
 - rm -rf node_modules/
Pella answered 16/5, 2021 at 9:42 Comment(1)
Is the rm -rf ... command for Linux or Unix? I'm on Windows, it won't seem to work for me, I get an error A parameter cannot be found that matches parameter name 'rf'Subprincipal
E
8

You can also run the following as shorthand:

npm un packageName or npm rm packageName

Note: Add -g at end of command to uninstall global packages.

Erythropoiesis answered 15/5, 2019 at 8:53 Comment(0)
C
6

I found this out the hard way, even if it is seemingly obvious.

I initially tried to loop through the node_modules directory running npm uninstall module-name with a simple for loop in a script. I found out it will not work if you call the full path, e.g.,

npm uninstall module-name

was working, but

npm uninstall /full/path/to/node_modules/module-name 

was not working.

Champ answered 1/3, 2016 at 21:49 Comment(0)
G
6

For Windows users - if you want to remove all the Node.js modules installed at once:

  • Open a PowerShell window
  • Go inside the node_modules folder (cd node_modules)
  • Run this command - "npm uninstall (Get-ChildItem).Name"

It will uninstall all the modules.

Geraud answered 25/5, 2017 at 11:6 Comment(0)
U
6

Simply,

npm un module_name

OR

npm uninstall module_name

Make sure you uninstall it in the same directory as the package.json and node_modules folder.

Unshapen answered 23/9, 2022 at 4:48 Comment(0)
M
2
# Log in as root (might be required depending on install)
su -

# List all global packages
npm ls -g --depth=0

# List all local (project) packages
npm ls -p --depth=0

# Remove all global packages
npm ls -g --depth=0 | awk -F/ '/node_modules/ && !/\/npm$/ {print $NF}' | xargs npm -g rm

# Remove all local packges
npm ls -p --depth=0 | awk -F/ '/node_modules/ && !/\/npm$/ {print $NF}' | xargs npm -p rm

# NOTE (optional): to use node with sudo you can add the bins to /usr/bin
# NOTE $PATHTONODEINSTALL is where node is installed (e.g. /usr/local/node)
sudo ln -s $PATHTONODEINSTALL/bin/node /usr/bin/node
sudo ln -s $PATHTONODEINSTALL/bin/npm /usr/bin/npm
Maretz answered 15/5, 2017 at 13:22 Comment(0)
A
2

The uninstall option didn't work for me when I tried to use the same command to the one I used in installing (as I was installing with the @latest directive)

So for example, I installed a package like this:

npm install  @ngtools/webpack@latest

And then I wanted to uninstall it, so I used the same command (including @latest):

npm uninstall  @ngtools/webpack@latest

So the above uninstall didn't work. I have to remove the @latest, and then it worked well:

npm uninstall  @ngtools/webpack
Aloud answered 25/10, 2018 at 13:30 Comment(0)
S
2

To uninstall a module using npm, you can use:

npm uninstall moduleName

Also, if you want to uninstall and want the change to be reflected in your package.json then you can use the --save flag, like this:

npm uninstall moduleName --save
OR
npm uninstall -S

And if you want to uninstall a module from devDependencies and want the change to be reflected in package.json then you can use -D flag, like this:

npm uninstall moduleName -D
Shropshire answered 11/4, 2019 at 12:28 Comment(0)
A
2

In npm v6+ npm uninstall <package_name> removes it both in folder node_modules and file package.json.

Akimbo answered 25/12, 2020 at 8:30 Comment(0)
P
1

Additionally, if you've started using yarn, in place of npm:
yarn remove <package-name>

Is the equivalent of:
npm uninstall <package-name> --save

This will
- remove the package from package.json, as well as
- uninstall it from your project's node-modules folder

Politburo answered 10/1, 2018 at 8:55 Comment(0)
A
1

The simplest solution is:

npm uninstall packageName --save-dev

See upper level packages names in the your project:

npm list --depth=0

The output will be like:

[email protected] /home/jackkobec/projects/myAppName
├── packageName@packageVersion
├── [email protected]

Copy package name and execute npm uninstall command. Example for express package:

npm uninstall express --save-dev
Alum answered 23/2, 2019 at 0:3 Comment(0)
C
1

If you want to uninstall an specific package using npm, you can use the following command :

Sintax:

npm uninstall <package-name>

Example:

npm uninstall moment
Cromer answered 25/3, 2022 at 21:28 Comment(2)
If you can keep the config in your package.json file in the dependencies property , you need to use the following command: npm uninstall webpack --no-saveCromer
Why did you post this answer when there were already many other answers saying the same thing?Lusterware
P
0

If you want to uninstall a number of modules, then just run the npm uninstall.

Then go to file package.json and delete the unwanted module from there, and then just run the command npm install. It should fix your problem.

Pompidou answered 27/12, 2017 at 11:51 Comment(0)
P
0

In case you are on Windows, run CMD as administrator and type:

npm -g uninstall <package name>
Pyretotherapy answered 22/1, 2020 at 19:22 Comment(0)
R
0

You can delete a Node.js module manually. For Windows,

  1. Go to the node_modules directory of your repository.

  2. Delete the Node.js module you don't want.

  3. Don't forget to remove the reference to the module in your package.json file! Your project may still run with the reference, but you may get an error. You also don't want to leave unused references in your package.json file that can cause confusion later.

Rodrique answered 5/6, 2020 at 21:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.