Ever wonder if your Angular project is bloated with unused components, directives, pipes, or services? Say hello to ngx-unused
โ a powerful CLI tool that helps you find and eliminate unused Angular classes in your codebase.
If you're working on a large codebase or an Nx monorepo, ngx-unused
can be a game-changer for code hygiene and performance.
๐ What is ngx-unused
?
ngx-unused
is a utility that scans your Angular project and detects declared but unused classes that are decorated with:
@Component
@Directive
@Pipe
@Injectable
It analyzes the usage of these classes across your project and flags anything that isn't actually used โ helping you identify dead code that can be removed safely.
๐ Installation & Usage
You donโt even need to install it globally. Just run:
npx ngx-unused <directory> -p <tsconfig-path>
๐ง Options:
Option | Description |
---|---|
<directory> |
Directory (or directories) to scan. You can pass multiple by separating with space. |
-p , --project
|
Path to your main tsconfig file. For Nx projects, this is usually tsconfig.base.json . |
-h , --help
|
Show help message. |
๐ Note: The source root(s) and the tsconfig
file must live under the same root directory.
๐งช Examples
Scan everything:
npx ngx-unused . -p tsconfig.base.json
Scan only specific libraries and apps:
npx ngx-unused libs apps/my-app -p tsconfig.base.json
๐ How Does It Work?
- It finds all classes decorated with Angular decorators (
@Component
,@Directive
,@Pipe
,@Injectable
). -
It checks if those classes are actually used in:
- TypeScript code
- Module metadata (
declarations
,providers
, etc.)
-
It ignores:
- Test files (
*.spec.ts
) - Simple imports/exports
- Usages in
useClass
oruseExisting
(these are considered used)
- Test files (
If a class isn't referenced in any meaningful way, it gets flagged.
๐ฆ Output
Results are printed in the terminal, grouped by file. Here's an example:
โ Scanning source files...
โ Unused Services:
- src/app/shared/my-unused.service.ts โ MyUnusedService
- libs/util/logger.service.ts โ LoggerService
โ Exit Codes
Code | Meaning |
---|---|
0 | No unused classes detected |
1 | Unused component/directive/pipe/service found |
2 | Invalid configuration |
๐ก Why You Should Use It
- Keep your codebase lean and maintainable
- Improve performance by eliminating dead code
- Catch forgotten legacy files that are no longer needed
- Great companion during refactors or audits
- Simplify Angular version upgrades โ removing unused code helps reduce complexity and potential migration issues when updating Angular versions (especially major ones)
๐งผ Conclusion
ngx-unused
is a minimalistic yet incredibly helpful tool for Angular developers looking to declutter their projects. Whether youโre working on a growing monorepo or tidying up a legacy app, itโs a must-have in your toolbox.
Give it a try:
npx ngx-unused . -p tsconfig.base.json
Let your Angular codebase breathe!
โ๏ธ Have you used ngx-unused
? Got tips or improvements to suggest? Drop a comment below!
Top comments (0)