delete-js-comments
v1.1.6
Published
A lightweight package to efficiently remove all types of comments from JavaScript code
Maintainers
Readme
delete-js-comments
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-commentsOr install globally to use as a CLI tool:
npm install -g delete-js-commentsQuick 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.jsOr install globally:
npm install -g delete-js-comments
# Then use directly
delete-js-comments input.js --overwrite
delete-js-comments input.js output.jsCLI 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.jsBasic 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,.cjsfiles - Sensitive File Blocking - Prevents modification of:
- Configuration files (
package.json,.env, etc.) - Build/dependency files (
node_modules,dist, etc.) - Version control files (
.git,.gitignore)
- Configuration files (
- 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 stringError: 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:
- Tracks whether we're inside a string literal
- Detects comment start sequences (
//or/*) - Removes comments while preserving code structure
- Handles escape sequences in strings correctly
- 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.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - 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.
