node.js - package How to uninstall npm modules in node js?



7 Answers

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

uninstall npm windows

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?




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.




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



Use npm uninstall <packageName> --save to uninstall a package and remove it's entry in package.json.

npm uninstall -g <packageName> --save will uninstall the package if it was added globally.




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




Sometimes npm uninstall -g packageName doens't work.

In this case you can delete package manually.

On Mac go to folder /usr/local/lib/node_modules and delete folder with package you want. That's it. Check your list of globally installed packages with this command npm list -g --depth=0




Related


Tags

node.js   npm