Finding dead code in a Node.js project

Nafis Fuad Name

Nafis Fuad


Wednesday, August 17, 2022

2 min read

In a project that’s mature enough, there’s always going to be some inevitable dead code, dangling files, and unused dependencies. Depending on the size and phase of the project, it’s practically impossible to manually go over every file and clean things up. There must be some tools for this…

cover image

Recently had to cleanup a lot of dead code that got accumulated in a large codebase over a few quarters. Agreed that it’s pretty bad to let a lot of dangling files, unused dependences and unused imports accumulate in the first place, but sometimes it’s inevitable given some time constraints and tight resources. The bigger problem imho is not going back to clean up the mess because they cause problems in the long run.

Since I’ve had to do this a couple of times already I can tell that it’s not a pleasant experience as it involves a lot of grunt work and removing the wrong file/package can end up costing hours. I believe the reasons behind some reluctance on the developers’ part might be summarized as -

a) it’s a lot of grunt work

b) it’s error-prone as there’s not enough tooling for this and

c) it doesn’t move any needle (in the short run)

Wished some more focus were put into this. Would love to see this as a feature of all the existing linters someday but that seems to be far away. Here’s a few CLI tools that are quite helpful and I’ve used them with great success 👇

  • Unimported

    Using it is a breeze. According to the docs: “unimported analyzes your code by following the require/import statements starting from your entry file.”

    Given that the root of the project is defined properly, one can fire up the terminal and hit:

    npx unimported

    Link to docs here.

  • Depcheck

    This is a tool to find out which dependencies are useless and which ones are missing from the package.json. Not sure when the later part might be useful because there’s gonna be errors when there unmet dependencies, but the former part is good ^^

    Using this similar to the other one:

    npx depcheck

    Link to docs here.

Hope this little write up helps. Will try to update this if/when I find something new. May the dead code be with you 😛

*a lil meme made by me for you.

Subscribe to Saturday Already!💌

Where should I send you all the free goodies?

No fluff. No spam. Unsubscribe any time.

    Every Saturday I share some high-quality insights from across the web, directly to you. Here's what you can expect:

  • Actionable productivity tips, some practical life lessons from my own mistakes, insights from the tech industry, and more.
  • Early previews of new products, services, resources etc. that I try out or that I've been working on myself.
  • Some great articles / books / videos / podcasts curated through a rigid filtration process over the past few years

Made with ❤️ and these tech.

Please don't copy me. 🙏