In this lesson, we'll apply our jq skills and write a script that gets a list of dependencies from package.json and greps for usages of every package. If a package is unused, it's marked for removal.
Get 'dependencies' from package.json
jq '.dependencies' package.json
## {"express": "^4.16.4", "lodash": "^4.17.11"}
Only get keys for dependencies:
jq '.dependencies | keys | .[]' package.json
## "express"
## "lodash"
Then drop the `"``
jq -r '.dependencies | keys | .[]' package.json
## express
## lodash
Creat check-unused-deps.sh
:
for dep in $(jq -r '.dependencies | keys | .[]' package.json); do
if ! grep "require(.*$dep.*)" -Rq --exclude-dir="node_modules" .; then
echo "You can probably remove $dep"
fi
done
Give premission:
chmod +x check-unused-deps.sh
After running, it shows which deps can be removed for node applications.