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

Read previous snippet: groom_dead_code()

Read next snippet: hasInternetAccess()