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

remove-code-comments

v0.1.11

Published

A lightweight CLI to remove JS/TS comments (usable as an npm package).

Readme

remove-code-comments

npm version npm downloads npm monthly downloads npm total downloads

A lightweight tool to remove comments from JavaScript/TypeScript files while preserving strings, regex literals, and template literals. Works as both a CLI and a programmatic library.

Features

Smart Comment Removal

  • Removes // line comments and /* */ block comments
  • Preserves comments inside strings, template literals, and regex patterns
  • Handles edge cases like regex literals after return, throw, etc.

Install

npm install -g remove-code-comments

Or use via npx:

npx remove-code-comments --help

For programmatic usage:

npm install remove-code-comments

CLI Usage

Interactive Mode

remove-code-comments --dir src

This will scan the directory and let you select which files to process:

Found 4 files (2 with comments):

  ✓ 1. src/index.ts (272 chars)
  ○ 2. src/utils.ts
  ✓ 3. src/api.ts (145 chars)
  ○ 4. src/types.ts

Enter comma-separated indices to strip comments (or 'all'): 1,3

Automatic Mode

Process all files automatically:

remove-code-comments --dir src --all

Dry Run

Preview changes without modifying files:

remove-code-comments --dir src --all --dry-run

Ignore Patterns

Skip specific files or directories:

remove-code-comments --dir src --all --ignore "**/*.test.ts,**/vendor/**"

Options

  • --dir, -d <folder>: Target directory (default: src)
  • --all: Process all matching files automatically (no interactive prompt)
  • --dry-run: Show what would change without writing files
  • --ignore <pattern,...>: Comma-separated ignore patterns (supports * and **)

Programmatic API

Use remove-code-comments as a library in your Node.js projects:

const { stripComments } = require("remove-code-comments");

const code = `
  // This is a line comment
  const x = 5; /* block comment */
  const regex = /https?:\\/\\//; // URL regex
  const str = "// not a comment";
`;

const clean = stripComments(code);
console.log(clean);

Output:



  const x = 5;
  const regex = /https?:\\/\\//;
  const str = "// not a comment";

API Reference

stripComments(code: string): string

Removes all comments from the provided code string.

Parameters:

  • code (string): JavaScript or TypeScript source code

Returns:

  • (string): Code with comments removed

Example:

const { stripComments } = require("remove-code-comments");

const result = stripComments("const x = 5; // comment");
// result: 'const x = 5; '

Examples

Example 1: Basic Comments

Input:

// Single line comment
const greeting = "Hello"; /* inline block comment */

/*
 * Multi-line
 * block comment
 */
function sayHi() {
  return greeting; // return value
}

Output:

const greeting = "Hello";

function sayHi() {
  return greeting;
}

Example 2: Preserving Strings and Regex

Input:

const url = "https://example.com"; // URL string
const pattern = /\/\* not a comment \*\//; // regex literal
const msg = "Use // for comments"; /* explanation */

Output:

const url = "https://example.com";
const pattern = /\/\* not a comment \*\//;
const msg = "Use // for comments";

Example 3: Template Literals

Input:

const code = `
  // This looks like a comment
  /* but it's inside a template */
`;
const value = 42; // actual comment

Output:

const code = `
  // This looks like a comment
  /* but it's inside a template */
`;
const value = 42;

Example 4: Complex Regex Context

Input:

function test() {
  return /regex/gi; // regex after return
  throw /pattern/; // regex after throw
  const result = condition ? /yes/ : /no/; // ternary
  const arr = [/first/, /second/]; // array literal
}

Output:

function test() {
  return /regex/gi;
  throw /pattern/;
  const result = condition ? /yes/ : /no/;
  const arr = [/first/, /second/];
}

License

MIT