npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

ts-code-line-counter

v1.0.4

Published

A TypeScript library to count lines of code in a codebase with beautiful CLI output

Readme

Author: Ankush Mehra
Repository: GitHub - ankushmehra/ts-code-line-counter

TypeScript Code Line Counter

A robust and type-safe npm library for counting lines of code in your projects, built with TypeScript.

Features

  • Type Safety: Built with TypeScript for better development experience
  • Accurate Counting: Count lines of code, comments, and empty lines
  • Customizable Filters: Filter by file extensions and exclude specific directories
  • Command Line Interface: Easy-to-use CLI tool
  • Detailed Reports: Get breakdowns by file type
  • Multiple Language Support: Works with JavaScript, TypeScript, Python, Java, and more

Installation

As a dependency in your project

npm install ts-code-line-counter --save

As a global CLI tool

npm install -g ts-code-line-counter

Usage

As a library in TypeScript

import { countLines } from 'ts-code-line-counter';

// Count lines in the current project
const result = countLines('.');

if (result) {
  console.log(`Total lines: ${result.totalLines}`);
  console.log(`Code lines: ${result.codeLines}`);
  console.log(`Comment lines: ${result.commentLines}`);
  console.log(`Empty lines: ${result.emptyLines}`);
}

// With options
const options = {
  include: ['.js', '.ts'],           // Only include JavaScript and TypeScript files
  exclude: ['node_modules', '.git'], // Exclude these directories
  countEmptyLines: false,            // Don't count empty lines
  countCommentLines: false,          // Don't count comment lines
  recursive: true                    // Scan directories recursively
};

const customResult = countLines('./src', options);

As a library in JavaScript

const { countLines } = require('ts-code-line-counter');

// Usage is the same as in TypeScript
const result = countLines('./src');

if (result) {
  console.log(`Total lines: ${result.totalLines}`);
  // ...
}

As a CLI tool

# Count lines in the current directory
ts-code-line-counter

# Count lines in a specific directory
ts-code-line-counter ./src

# Count only specific file types
ts-code-line-counter --include js,ts,jsx

# Exclude specific directories
ts-code-line-counter --exclude node_modules,dist,build

# Don't count empty lines
ts-code-line-counter --no-empty

# Don't count comment lines
ts-code-line-counter --no-comments

# Don't search recursively
ts-code-line-counter --no-recursive

# Show help
ts-code-line-counter --help

Supported File Types

By default, the library supports the following file extensions:

  • JavaScript: .js, .jsx
  • TypeScript: .ts, .tsx
  • Python: .py
  • Java: .java
  • C/C++: .c, .cpp
  • C#: .cs
  • HTML: .html
  • CSS: .css
  • PHP: .php
  • Go: .go
  • Swift: .swift
  • Ruby: .rb

You can specify your own list of extensions using the include option.

API Reference

Types

interface CountOptions {
  include: string[];            // File extensions to include
  exclude: string[];            // Paths to exclude
  countEmptyLines: boolean;     // Whether to count empty lines
  countCommentLines: boolean;   // Whether to count comment lines
  recursive: boolean;           // Whether to search directories recursively
}

interface CountResult {
  totalLines: number;           // Total number of lines
  codeLines: number;            // Number of code lines
  commentLines: number;         // Number of comment lines
  emptyLines: number;           // Number of empty lines
  fileBreakdown: Record<string, FileCountResult>; // Breakdown of lines by file
}

interface FileCountResult {
  totalLines: number;           // Total number of lines in the file
  codeLines: number;            // Number of code lines in the file
  commentLines: number;         // Number of comment lines in the file
  emptyLines: number;           // Number of empty lines in the file
}

Functions

countLines(targetPath: string, options?: Partial<CountOptions>): CountResult | null

Counts lines in a file or directory.

  • targetPath (string): Path to the file or directory
  • options (Partial<CountOptions>, optional): Configuration options
  • Returns: CountResult object or null if the path cannot be processed

countLinesInFile(filePath: string, options?: Partial<CountOptions>): FileCountResult | null

Counts lines in a single file.

  • filePath (string): Path to the file
  • options (Partial<CountOptions>, optional): Configuration options
  • Returns: FileCountResult object or null if the file cannot be read

countLinesInDirectory(dirPath: string, options?: Partial<CountOptions>): CountResult

Counts lines in files within a directory.

  • dirPath (string): Path to the directory
  • options (Partial<CountOptions>, optional): Configuration options
  • Returns: CountResult object with aggregated statistics

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Run the tests (npm test)
  4. Commit your changes (git commit -m 'Add some amazing feature')
  5. Push to the branch (git push origin feature/amazing-feature)
  6. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.