Code Bloat or refactor: Simple Ways to Measure Your Project

How to Count Lines of Code in Your Project: A Developer's Guide to Project Analysis
When you're deep in development mode, sometimes you pause and wonder: "Just how big is this codebase I'm managing?" Whether it's a work project or a side project you've been building with Cursor (which probably has some unnecessary code), knowing your project's size helps you make informed decisions about refactoring, maintenance, and complexity management.
While counting lines of code isn't the most accurate way to measure productivity or code quality, it's incredibly useful for getting a feel for your project's scope and identifying areas that might need refactoring to reduce bloat.
The Quick and Dirty Approach: PowerShell One-Liner
If you need a rapid count of your TypeScript and JavaScript files, here's a simple PowerShell command:
Get-ChildItem -Recurse -Include *.ts,*.tsx,*.js,*.jsx | Get-Content | Measure-Object -Line
The Problem: This approach is like using a chainsaw to trim a bonsai tree. It counts everything—blank lines, comments, and even files in node_modules
or .next
directories. Plus, on large projects, it can make your terminal feel sluggish.
Let's do better.
The Professional Way: Using cloc
or tokei
For accurate, meaningful code analysis, use dedicated tools like cloc
(Count Lines of Code) or tokei
. These tools provide detailed breakdowns and exclude irrelevant content.
Option 1: Using cloc
cloc
is a mature, reliable tool that's been around for years. Here's how to get started:
Step 1: Install cloc
Choose your preferred installation method:
npm install -g cloc # Node.js method
choco install cloc # Chocolatey (Windows)
brew install cloc # Homebrew (macOS)
Step 2: Run It
Navigate to your project directory and run:
cloc .
Sample Output:
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
TypeScript 30 400 200 3500
JavaScript 5 80 40 600
-------------------------------------------------------------------------------
You get the real deal—actual code lines, properly categorized.
Pro tip: If cloc
isn't working in VS Code on Windows, try running it in Git Bash or PowerShell directly in your project folder.
Excluding Unwanted Directories
To avoid counting build artifacts, dependencies, or editor configurations:
cloc . --exclude-dir=node_modules,.next,.cursor
Or exclude all hidden directories (like .git
, .vscode
, etc.):
cloc . --not-match-d='^\.'
Option 2: Using Tokei (The Speed Demon)
If you're working with a massive codebase and need results fast, Tokei is your friend. Written in Rust, it's blazingly fast.
Installation and Usage:
Visit Tokei's GitHub repository for installation instructions.
Basic usage:
tokei .
Excluding directories:
tokei . --exclude node_modules --exclude .next --exclude .cursor
Automating the Process
Add these scripts to your package.json
for easy access:
{
"scripts": {
"count-lines": "cloc . --exclude-dir=node_modules,.next,.cursor",
"count-tokei": "tokei . --exclude node_modules --exclude .next --exclude .cursor"
}
}
Now you can run:
npm run count-lines
Perfect for CI pipelines, code audits, or regular project health checks.
Quick Reference Commands
cloc:
cloc . --exclude-dir=node_modules,.next,.cursor
Tokei:
tokei . --exclude node_modules --exclude .next --exclude .cursor
PowerShell (Quick & Dirty):
Get-ChildItem -Recurse -Include *.ts,*.tsx,*.js,*.jsx | Get-Content | Measure-Object -Line
Final Thoughts
Remember: lines of code ≠ better code. Use these tools to develop intuition about your project's complexity, identify refactoring opportunities, and maintain a healthy codebase. Whether you choose cloc
for its maturity or tokei
for its speed, you'll have the insights you need to make informed decisions about your code.
Happy counting! 📊