Remove all npm modules globally

The command line given below will remove all globally installed npm modules.

npm ls -gp --depth=0 | awk -F/ '/node_modules/ && !/\/npm$/ {print $NF}' | xargs npm -g rm
npm ls -gp --depth=0 
The above command in the command line will list all top level modules.
awk -F/ '/node_modules/ && !/\/npm$/ {print $NF}'
This will print all modules that are not actually npm itself.
xargs npm -g rm 
- removes all modules globally. Windows users can directly delete the contents of AppData. You can go to AppData folder by typing %appdata% in the explorer or run prompt or start menu. Another solution is to find all globally installed modules with npm -g ls command and remove them using npm -g rm.