A fast and flexible CLI tool to remove comments from JavaScript/TypeScript files while preserving important ones
npm install remove-comments-cli




A fast and flexible CLI tool to remove comments from JavaScript/TypeScript files while preserving important ones.
- 🚀 Fast and memory-efficient comment removal
- 💡 Preserve important comments using markers
- 🌟 Support for JavaScript, TypeScript, and JSX
- 🔍 Smart handling of strings, regex, and JSX syntax
- 📁 Process multiple files using glob patterns
- 🎯 Configurable output directory
``bash`
npm install remove-comments-cli --save-dev
Remove all comments from a file:
`bash`
npx remove-comments input.ts > output.ts
Process multiple files using glob patterns:
`bash`
npx remove-comments "src/*/.{js,ts,jsx,tsx}" --outDir dist
By default, comments starting with ! are preserved:
`ts`
// This comment will be removed
//! This important comment will be preserved
/ This block comment will be removed /
/! This important block comment will be preserved /
You can specify custom markers:
`bash`Preserve comments starting with # or *
npx remove-comments input.ts -k "#*" > output.ts
`bash
npx remove-comments --help
Options:
-k, --keep-markers
-o, --outDir
-v, --verbose Print detailed processing information
-h, --help Display help information
`
Process TypeScript files and preserve comments starting with ! or #:
`bash`
npx remove-comments "src/*/.ts" -k "!#" --outDir dist
Process JSX/TSX files and preserve comments starting with *:
`bash`
npx remove-comments "src/*/.{jsx,tsx}" -k "*" --outDir dist
Process a single file and output to stdout:
`bash`
npx remove-comments input.js > output.js
The tool is optimized for performance and memory efficiency:
- ⚡ Processes ~1000 files/second on modern hardware
- 📉 Low memory footprint (~10MB for 1000 files)
- 🔄 Streaming file processing for large files
- 💻 Multi-threaded processing for large directories
| Node.js Version | Support Status |
|----------------|----------------|
| 20.x | ✅ Full |
| 18.x (LTS) | ✅ Full |
| 16.x | ❌ Not supported|
Tested on:
- Windows 10/11
- macOS 10.15+
- Ubuntu 20.04+
1. "Error: ENOENT: no such file or directory"
- Check if the file paths are correct
- Ensure glob patterns are quoted in the shell
- Use forward slashes (/) even on Windows
2. "SyntaxError: Unexpected token"
- Verify the input file is valid JavaScript/TypeScript
- Check for malformed comments or JSX syntax
- Try running with --verbose for more details
3. "Error: Cannot find module"
- Run npm install remove-comments-cli again
- Check Node.js version compatibility
- Verify package.json dependencies
Run with debug logging enabled:
`bash``
DEBUG=remove-comments* npx remove-comments input.ts
- No eval or dynamic code execution
- No external network calls
- Input validation for all file paths
- Safe handling of symbolic links
We welcome contributions! Please see our Contributing Guide for details.
MIT