[Node.js] how to uninstall npm modules in node js?


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>.


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 anymore 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?

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

This will remove those packages, which exist in the filesystem, but are not used/declared 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".

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.

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

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.

# login 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

If to want to uninstall a number of module the just run the npm uninstall. Then go to package.json and delete the unwanted module from there, and then just run the command npm install . It should fix your problem.