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 🙏

© 2026 – Pkg Stats / Ryan Hefner

comment-bear

v1.1.0

Published

Universal comment remover for multiple programming languages with TypeScript support

Readme

GitHub npm Tests License

🐻 A fast and friendly tool for removing comments from code in multiple programming languages. Built with TypeScript and thoroughly tested with 1043+ tests to ensure reliability and quality.

✨ Features

  • 🌐 Language Support:

    • Full Support: JavaScript, TypeScript, Python, Java, C#, C, C++, HTML, CSS, SQL, Ruby, Haskell
    • Basic Support (using generic comment patterns): PHP, Go, Rust, Swift, Kotlin, Scala, YAML, JSON, XML
    • More languages coming soon!
  • 🔍 Automatic language detection by file extension or content

  • 📝 Preserve license comments (optional)

  • 🔒 Type safety with full TypeScript support

  • High performance with minimal dependencies

  • 🧪 Dry-run mode for preview

  • 📦 Easy integration with Node.js and TypeScript projects

  • 🖥️ CLI tool for command-line usage

  • 🌊 Stream API for processing large files

  • ⚙️ Configuration files (.commentbearrc) for project-level settings

Note on Language Support:

  • Full Support: Dedicated comment remover with language-specific rules
  • Basic Support: Uses generic comment patterns that work for most cases but might not handle all edge cases
  • We're actively working on adding more languages and improving existing support

📦 Installation

npm install comment-bear

🚀 Quick Start

TypeScript

import { removeComments } from 'comment-bear';

const code = `
// This is a comment
const hello = () => {
  console.log("Hello World"); // Inline comment
};
`;

const result = removeComments(code, { language: 'javascript' });
console.log(result.code);
// Output:
// const hello = () => {
//   console.log("Hello World");
// };

JavaScript (CommonJS)

const { removeComments } = require('comment-bear');

const code = '# Python comment\nprint("Hello")';
const result = removeComments(code, { language: 'python' });
console.log(result.code); // print("Hello")

JavaScript (ES Modules)

import { removeComments } from 'comment-bear';

const result = removeComments(myCode, { filename: 'script.js' });

📖 API Documentation

removeComments(code: string, options?: RemoveOptions): RemoveResult

Main function for removing comments.

Parameters

  • code (string): Input code to process
  • options (RemoveOptions, optional): Configuration options

RemoveOptions

interface RemoveOptions {
  language?: Lang;              // Explicitly specify language
  filename?: string;            // Filename for auto-detection
  preserveLicense?: boolean;    // Preserve license comments (default: false)
  dryRun?: boolean;            // Test mode without changes (default: false)
  keepEmptyLines?: boolean;    // Preserve empty lines (default: false)
}

RemoveResult

interface RemoveResult {
  code: string;                 // Processed code
  removedCount: number;         // Number of comments removed
  detectedLanguage?: Lang;      // Auto-detected language
}

Supported Languages (Lang)

type Lang =
  | "javascript" | "typescript" | "python" | "ruby"
  | "java" | "csharp" | "c" | "cpp"
  | "html" | "css" | "sql" | "yaml"
  | "json" | "xml" | "php" | "go"
  | "rust" | "swift" | "kotlin" | "scala"
  | "haskell";

🎯 Usage Examples

Automatic Language Detection

import { removeComments } from 'comment-bear';

// By filename
const result1 = removeComments(code, { filename: 'script.py' });
console.log(result1.detectedLanguage); // "python"

// By content
const htmlCode = '<!DOCTYPE html><!-- Comment --><html></html>';
const result2 = removeComments(htmlCode);
console.log(result2.detectedLanguage); // "html"

Preserving License Comments

const code = `
/*! MIT License - Copyright (c) 2025 */
// Regular comment
const x = 5;
`;

const result = removeComments(code, {
  language: 'javascript',
  preserveLicense: true
});

console.log(result.code);
// Output:
// /*! MIT License - Copyright (c) 2025 */
// const x = 5;

Dry-run Mode

const code = '// Comment\nconst x = 5;';

const result = removeComments(code, {
  language: 'javascript',
  dryRun: true
});

console.log(result.code === code); // true (code was not modified)
console.log(result.removedCount);  // 1 (number of comments that would be removed)

Working with Different Languages

Python

const pythonCode = `
# This is a comment
def hello():
    """Docstring"""
    print("Hello")  # Inline comment
`;

const result = removeComments(pythonCode, { language: 'python' });

Java

const javaCode = `
// Single line comment
/* Multi-line
   comment */
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello"); // Inline
    }
}
`;

const result = removeComments(javaCode, { language: 'java' });

HTML

const htmlCode = `
<!-- This is a comment -->
<div class="container">
  <!-- Another comment -->
  <p>Content</p>
</div>
`;

const result = removeComments(htmlCode, { language: 'html' });

SQL

const sqlCode = `
-- Single line comment
/* Multi-line
   comment */
SELECT * FROM users;
`;

const result = removeComments(sqlCode, { language: 'sql' });

Kotlin

const kotlinCode = `
// Data class comment
data class User(
    val name: String, // User name
    val age: Int // User age
)
`;

const result = removeComments(kotlinCode, { language: 'kotlin' });

Scala

const scalaCode = `
// Trait definition
trait Greeter {
  def greet(name: String): Unit // greet method
}
`;

const result = removeComments(scalaCode, { language: 'scala' });

Haskell

const haskellCode = `
{-# LANGUAGE OverloadedStrings #-}
-- | Main module
module Main where

-- Entry point
main :: IO ()
main = putStrLn "Hello" -- prints greeting
`;

const result = removeComments(haskellCode, { language: 'haskell' });
// Pragmas ({-# #-}) are always preserved

🖥️ CLI Usage

# Install globally
npm install -g comment-bear

# Remove comments and print to stdout
comment-bear src/index.js

# Write to output file
comment-bear src/index.js -o clean.js

# Modify file in place
comment-bear src/index.js -i

# Multiple files in place
comment-bear src/*.js -i

# Force language detection
comment-bear config.txt --language javascript

# Preserve license comments
comment-bear src/index.js --preserve-license

# Preview what would be removed
comment-bear src/index.js --dry-run

# Use a config file
comment-bear src/index.js --config .commentbearrc

CLI Options

| Option | Short | Description | |---|---|---| | --help | -h | Show help message | | --version | -v | Show version number | | --output <file> | -o | Write output to file | | --in-place | -i | Modify files in place | | --language <lang> | -l | Force language | | --preserve-license | | Keep license comments | | --dry-run | | Preview without modifying | | --keep-empty-lines | | Preserve empty lines | | --config <path> | -c | Path to config file |

🌊 Stream API

For processing large files efficiently:

import { createCommentRemoverStream } from 'comment-bear';
import { createReadStream, createWriteStream } from 'fs';

// Pipe a file through the comment remover
createReadStream('input.js')
  .pipe(createCommentRemoverStream({ language: 'javascript' }))
  .pipe(createWriteStream('output.js'));

// With options
createReadStream('input.js')
  .pipe(createCommentRemoverStream({
    language: 'javascript',
    preserveLicense: true,
    keepEmptyLines: true,
  }))
  .pipe(createWriteStream('output.js'));

// Auto-detect language by filename
createReadStream('input.py')
  .pipe(createCommentRemoverStream({ filename: 'input.py' }))
  .pipe(createWriteStream('output.py'));

⚙️ Configuration Files

Create a .commentbearrc or .commentbearrc.json file in your project root:

{
  "language": "javascript",
  "preserveLicense": true,
  "keepEmptyLines": false,
  "exclude": ["node_modules", "dist"],
  "include": ["src/**/*.ts"]
}

The CLI automatically discovers config files by walking up the directory tree from the current working directory.

Programmatic usage:

import { loadConfig, mergeConfig } from 'comment-bear';

// Auto-discover and load config
const config = loadConfig();

// Load from specific path
const config2 = loadConfig('.commentbearrc');

🧪 Testing

# Run tests
npm test

# Run tests with coverage
npm test -- --coverage

# Watch mode
npm test -- --watch

🏗️ Development

# Clone the repository
git clone https://github.com/ivan-markov-666/comment-bear.git
cd comment-bear

# Install dependencies
npm install

# Build
npm run build

# Watch mode for development
npm run dev

📁 Project Structure

comment-bear/
├── src/
│   ├── index.ts              # Main entry point & API exports
│   ├── types.ts              # TypeScript types
│   ├── cli.ts                # CLI tool
│   ├── stream.ts             # Stream API
│   ├── config.ts             # Configuration file support
│   ├── detectors/            # Language detectors
│   │   └── language-detector.ts
│   └── removers/             # Language-specific removers
│       ├── javascript-remover.ts
│       ├── python-remover.ts
│       ├── css-html-remover.ts
│       ├── sql-remover.ts
│       ├── c-style-remover.ts  # Java, C#, C, C++, PHP, Go, Rust, Swift, Kotlin, Scala
│       └── other-remover.ts    # JSON, YAML, Ruby, Haskell
├── test/                     # Tests (1043+ test cases)
├── dist/                     # Compiled files (auto-generated)
├── package.json
├── tsconfig.json
└── README.md

🤝 Contributing

Contributions are welcome! Please follow these steps:

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

📝 License

MIT License - see the LICENSE file for details.

🙏 Acknowledgments

📞 Contact

🗺️ Roadmap

  • [x] CLI tool
  • [x] Support for more languages (Kotlin, Scala, Haskell)
  • [x] Stream API for large file processing
  • [x] Configuration files (.commentbearrc)
  • [ ] Editor plugins (VS Code, IntelliJ)
  • [ ] GitHub Action for automatic comment removal

⭐ If you find this project useful, give it a star on GitHub!