<Back

Maintaining Javascript repository health_

Today I will be going over what I believe is an essential task for maintaining a Javascript repository. That task is maintaining the health of your repository.

What do I mean by repository health? I mean ensuring that your repository is in a state where it is easy to work with, easy to contribute to, and easy to maintain.

Here are some things you can do to maintain the health of your repository:

  1. Keep your dependencies up to date - By keeping your dependencies up to date, you ensure that you are using the latest and greatest features and bug fixes. I have already written a blog post on this topic, which you can find here.
  2. Use a linter - A linter is a tool that analyzes your code and flags any potential issues. By using a linter, you can ensure that your code is consistent and follows best practices. I recommend using ESLint and Prettier.
  3. Documentation - Documentation is essential for maintaining a healthy repository. By documenting your code, you make it easier for other developers to understand your codebase. Documentation comes in a variety of forms, such as comments in your code, README files, API documentation, and even Typescript types can be considered documentation.
  4. Removing dead code - Dead code is code that is no longer used. By removing dead code, you can make your repository easier to work with and maintain. For the remainder of this blog post, I will be focusing on the last point, removing dead code.

Tracking Unused Files and Exports

This is a very important task, as dead code can make your repository harder to work with and maintain. Dead code can also make it harder to understand your codebase, as it can be confusing to see code that is not being used.

When you are the sole contributor on a project, it is fairly easy to manage dead code. You are familiar with the codebase and can easily identify code that is no longer being used. However, when you are working on a team, it can be more difficult to manage dead code. Team members may be hesitant to remove code that they are not familiar with, as they may not know if it is being used by other parts of the codebase.

After trying out a few different approaches, I have found that the best way to manage dead code on a team is to use a tool called Knip. Knip is a tool that analyzes your codebase and identifies dead code. It then provides you with a report that you can use to remove the dead code.

Running Knip

The easiest way is to run:

npx knip

This will install Knip and run it on your codebase. Knip will analyze your codebase and provide you with a report including several categories:

  1. Unused files - Files that are not being imported or required by any other files.
  2. Unused exports - Exports that are not being imported by any other files.
  3. Unused dependencies - Dependencies that are not being used in your codebase.

Other things like unused variables, functions, and classes are typically covered by your linter, however Knip can also identify these in some cases.

Advanced Configuration

Knip can be configured to ignore certain files and directories. This can be useful if you have files loaded dynamically, or if you have exports that are used in other parts of your codebase that Knip is not able to detect.

To configure Knip, create a knip.json file in the root of your project. Here is an example configuration:

{
  "ignore": [
    // Ignore a file
    "./test.js",
    // Ignore a directory
    "../test/**"
  ]
}

Monorepo Support

Knip also supports monorepos. If you are using a monorepo, you can run Knip in the root of your monorepo and it will analyze all of the packages in your monorepo.

There is one downside to the Knip unused export tracking in the monorepo setup. It will not track exports that are used in other packages in the monorepo. This is a limitation of Knip, and I hope that it will be addressed in a future release.

Conclusion

Maintaining the health of your repository is an essential task for any developer. By keeping your repository in a healthy state, you make it easier to work with, contribute to, and maintain. I hope that this blog post has given you some ideas on how to maintain the health of your repository.