· From Managing npm dependencies
This groom_dependencies
Bash function performs loose grep checks to figure out whether package.json
dependencies are imported in JavaScript files (either with require
or import
). It can lead to false positives though, tread carefully.
Add this function to .zshrc
or .bashrc
:
function groom_dependencies {
key=${1:-dependencies}
for dep in $(cat package.json | jq -cr ".$key|keys|.[]");
do [[ -z "$(grep -r --exclude-dir=node_modules "'${dep}" .)" ]] && echo "$dep appears unused";
done
}
Then use it like this:
groom_dependencies devDependencies
groom_dependencies dependencies