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

delete-js-comments

v1.1.6

Published

A lightweight package to efficiently remove all types of comments from JavaScript code

Readme

delete-js-comments

npm version npm downloads License: MIT Node.js Version

A lightweight, fast, and secure package to remove all types of comments from JavaScript code


Features

  • Fast & Efficient - Optimized performance with array-based string building
  • Secure - Input validation and DoS protection built-in
  • Zero Dependencies - No external packages required
  • Simple API - Just one function, easy to use
  • ES6 Modules - Modern JavaScript support
  • Safe - Preserves comments inside string literals

Removes All Comment Types

  • Single-line comments (//)
  • Multi-line comments (/* */)
  • JSDoc comments (/** */)
  • Inline comments

Installation

npm install delete-js-comments

Or install globally to use as a CLI tool:

npm install -g delete-js-comments

Quick Start

As a Module

import deleteComments from 'delete-js-comments';

const code = `
// This is a comment
const x = 5; // inline comment

/* Multi-line
   comment */
function hello() {
  return "world";
}
`;

const cleanCode = deleteComments(code);
console.log(cleanCode);

As a CLI Tool

Recommended: Use npx (no installation required)

# Output to stdout
npx delete-js-comments input.js

# Save to output file
npx delete-js-comments input.js output.js

# Overwrite the input file
npx delete-js-comments input.js --overwrite

# Use with pipes
cat input.js | npx delete-js-comments > output.js

Or install globally:

npm install -g delete-js-comments

# Then use directly
delete-js-comments input.js --overwrite
delete-js-comments input.js output.js

CLI Usage

Installation Options

# Option 1: Use npx (no installation)
npx delete-js-comments file.js

# Option 2: Install globally
npm install -g delete-js-comments
delete-js-comments file.js

# Option 3: Install locally in project
npm install delete-js-comments
npx delete-js-comments file.js

Basic Commands

# Process a single file (output to stdout)
delete-js-comments src/app.js

# Save to a specific output file
delete-js-comments src/app.js dist/app.js

# Overwrite the original file
delete-js-comments src/app.js --overwrite

# Process all JavaScript files in a directory recursively
delete-js-comments src/

# Process a directory and save to output directory
delete-js-comments src/ dist/

Security Features

The CLI tool includes comprehensive security features:

  • Path Traversal Protection - Blocks attempts to access files outside the current directory
  • File Type Validation - Only processes .js, .jsx, .mjs, .cjs files
  • Sensitive File Blocking - Prevents modification of:
    • Configuration files (package.json, .env, etc.)
    • Build/dependency files (node_modules, dist, etc.)
    • Version control files (.git, .gitignore)
  • Size Limits - Files are limited to 10MB to prevent DoS attacks

API Reference

deleteComments(code)

Removes all types of comments from JavaScript code while preserving comments in string literals.

Parameters

  • code (string): The JavaScript code to process

Returns

  • (string): The code with all comments removed

Throws

  • TypeError: If the input is not a string
  • Error: If the input exceeds the 10MB size limit

Example

import deleteComments from 'delete-js-comments';

const jsCode = `
// This will be removed
const message = "// This will be preserved";

/**
 * This JSDoc will be removed
 */
function greet() {
  return message; /* This comment will be removed */
}
`;

const clean = deleteComments(jsCode);
console.log(clean);

Output:

const message = "// This will be preserved";

function greet() {
  return message; 
}

Security

This package includes several security features:

Input Validation

  • Type checking ensures only strings are processed
  • Size limit of 10MB per input to prevent DoS attacks

Memory Safety

  • Uses array-based string building to prevent memory exhaustion
  • Efficient algorithms to handle large files

Safe Processing

  • Preserves string literals to avoid breaking code
  • Handles edge cases safely

For more details, see SECURITY.md.


How It Works

The package uses a state machine approach to parse JavaScript code:

  1. Tracks whether we're inside a string literal
  2. Detects comment start sequences (// or /*)
  3. Removes comments while preserving code structure
  4. Handles escape sequences in strings correctly
  5. Maintains line breaks for better code structure

Limitations

  • Only processes JavaScript/JSX files (not TypeScript)
  • Does not handle template literals with nested comments
  • Assumes valid JavaScript syntax (won't parse broken code)

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. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

MIT © 2025

See LICENSE for more information.


Repository

https://github.com/benshabbat/deleteComments


Support

  • Report issues: https://github.com/benshabbat/deleteComments/issues
  • Star the project if you find it useful!

Related Packages


Changelog

See CHANGELOG.md for version history.