Cleaning up unused dependencies

Luis Rangel
2 min readJul 9, 2024

--

depcheck :D

Performing a dependency check and cleaning up unused dependencies is an important task to keep your application lightweight and maintainable. Here’s a step-by-step strategy to accomplish this:

  1. Install depcheck

First, install depcheck if you haven't already. This tool will help you identify unused dependencies in your project.

npm install -g depcheck

2. Run depcheck

Navigate to your project’s root directory and run depcheck.

depcheck

3. Review Results

depcheck will output a list of unused dependencies, missing dependencies, and dependencies that could not be analyzed. Pay close attention to the unused dependencies.

4. Verify Unused Dependencies

Before deleting anything, manually verify that the listed unused dependencies are not used in your project. Sometimes, depcheck might not catch usage in certain scenarios such as dynamic imports, or in configuration files.

5. Remove Unused Dependencies

For each verified unused dependency, remove it from your project.

npm uninstall <dependency-name>

6. Test the Application

After removing the dependencies, thoroughly test your application to ensure nothing breaks. This includes running your test suite and manually testing key functionalities.

7. Clean Up and Update

Optionally, you can clean your node_modules and re-install to ensure everything is in sync.

rm -rf node_modules
npm install

8. Commit Changes

Once you have verified that your application works without the removed dependencies, commit the changes.

git add package.json package-lock.json
git commit -m "fix: removed unused dependencies - <dependency-name>"

Additional Tips

  • Regular Maintenance: Perform this check periodically to keep your project clean.
  • Automated Tools: Consider integrating depcheck or similar tools into your CI/CD pipeline to automate this process.
  • Documentation: Document the process and the tools used for future reference and for other team members.
  • Backup: Before making major changes, ensure you have a backup or are working on a separate branch to avoid any disruptions.

Example Script

Here’s a small script to automate some of these steps:

# Install depcheck if not already installed
npm install -g depcheck

# Run depcheck and save output to a file
depcheck > depcheck_output.txt

# Review depcheck output manually
echo "Review depcheck_output.txt for unused dependencies."

# After manual review, remove each unused dependency
unused_deps=(dep1 dep2 dep3) # Replace with actual dependencies
for dep in "${unused_deps[@]}"; do
npm uninstall "$dep"
done

# Clean up node_modules and reinstall
rm -rf node_modules
npm install

# Test the application
npm test

echo "If everything works fine, commit your changes."

By following these steps, you can effectively clean up your project’s dependencies, making it more efficient and easier to maintain.

--

--

Luis Rangel

Hi I’m Luis Rangel, a Full Stack Developer and a newbie on a loop 🚀 from Guatemala, currently, I’m a Team Member Telus International🙍🏽‍♂️ @luisrangelc.