console-log-remove
v0.0.1
Published
Find and remove console.log statements - interactive or automatic
Maintainers
Readme
console-log-remove
Find and remove console.log statements from your code - safely and quickly
Problem
You have 500 console.log() scattered everywhere. Production deploy is in 10 minutes.
Solution
console-log-remove finds and removes console statements safely, with options to keep error/warn and create backups.
Features
- Find all console statements - Scans your entire codebase
- Keep what matters - Keep
console.errorandconsole.warnwith flags - Dry run mode - Preview changes before applying
- Automatic backups - Never lose code accidentally
- TypeScript + JavaScript - Works with .ts, .tsx, .js, .jsx, .mjs, .cjs
- Smart removal - Handles multi-line statements and removes empty lines
- Fast - Uses fast-glob for quick scanning
Installation
# Run directly with npx (recommended)
npx console-log-remove
# Or install globally
npm install -g console-log-remove
# Short alias
npx clrUsage
Preview (Dry Run)
# See what would be removed without making changes
npx console-log-remove --dry-run
# Preview in a specific directory
npx console-log-remove ./src --dry-runRemove All
# Remove all console statements
npx console-log-remove --remove-all
# Keep console.error and console.warn
npx console-log-remove --remove-all --keep-error --keep-warn
# Or use --keep flag
npx console-log-remove --remove-all --keep error,warn,infoStatistics Only
# Just show stats, don't remove anything
npx console-log-remove --statsExample Output
Scan Results
🔍 Found 47 console statements:
By type:
console.log: 35
console.error: 8
console.warn: 4
📁 src/utils/api.ts
L23: console.log('API response:', data)
L45: console.error('Error:', err)
L67: console.log('Request:', config)
📁 src/components/Login.tsx
L12: console.log('User:', user)
L34: console.warn('Deprecated prop used')
Options:
--remove-all Remove all console statements
--keep-error Keep console.error
--keep-warn Keep console.warn
--dry-run Preview without changes
Example:
npx console-log-remove --remove-all --keep-error --keep-warnDry Run
🔍 Dry Run - No files will be modified
📁 src/utils/api.ts
L23: console.log('API response:', data) [REMOVE]
L45: console.error('Error:', err) [KEEP]
📁 src/components/Login.tsx
L12: console.log('User:', user) [REMOVE]
L34: console.warn('Deprecated prop used') [KEEP]
──────────────────────────────────────────────────
Summary:
Would remove: 35
Would keep: 12
Run without --dry-run to apply changes.After Removal
✓ src/utils/api.ts
Removed: 2, Kept: 1
Backup: src/utils/api.ts.backup
✓ src/components/Login.tsx
Removed: 1, Kept: 1
Backup: src/components/Login.tsx.backup
✅ Removed 35 console statements.
Kept 12 statements (error/warn or skipped).Comparison with Alternatives
| Feature | console-log-remove | ESLint no-console | babel-plugin | Manual | |---------|-------------------|-------------------|--------------|--------| | Find statements | ✅ | ✅ | ❌ | ❌ | | Selective removal | ✅ | ⚠️ (config) | ❌ | ✅ | | Keep error/warn | ✅ | ⚠️ (config) | ⚠️ | ✅ | | Preview changes | ✅ | ❌ | ❌ | ❌ | | Automatic backup | ✅ | ❌ | ❌ | ❌ | | Statistics | ✅ | ❌ | ❌ | ❌ | | Multi-line support | ✅ | ✅ | ✅ | ⚠️ | | No config needed | ✅ | ❌ | ❌ | ✅ |
CLI Options
Usage: console-log-remove [options] [path]
Arguments:
path Directory to scan (default: ".")
Options:
--dry-run Preview changes without modifying files
--remove-all Remove all console statements
--keep-error Keep console.error statements
--keep-warn Keep console.warn statements
--keep <methods> Comma-separated methods to keep (e.g., error,warn)
--no-backup Do not create backup files
--include <patterns> Glob patterns to include (comma-separated)
--exclude <patterns> Glob patterns to exclude (comma-separated)
--json Output results as JSON
--stats Show statistics only
-V, --version Output version number
-h, --help Display helpSupported Console Methods
All console methods are detected:
console.log,console.debug,console.infoconsole.warn,console.errorconsole.trace,console.table,console.dirconsole.time,console.timeEnd,console.timeLogconsole.group,console.groupEnd,console.groupCollapsedconsole.count,console.countResetconsole.assert,console.clearconsole.profile,console.profileEnd
Programmatic Usage
import { scanDirectory, removeFromFile, filterStatements } from 'console-log-remove';
// Scan a directory
const result = await scanDirectory('./src');
console.log(`Found ${result.totalStatements} console statements`);
// Remove from specific file
for (const fileResult of result.files) {
// Keep error and warn
const toRemove = filterStatements(fileResult.statements, ['error', 'warn']);
if (toRemove.length > 0) {
removeFromFile(fileResult, toRemove, true); // true = create backup
}
}CI/CD Integration
# GitHub Actions - fail if console.log found
- name: Check for console statements
run: |
npx console-log-remove --stats --json > console-check.json
COUNT=$(cat console-check.json | jq '.byMethod.log // 0')
if [ "$COUNT" -gt 0 ]; then
echo "Found $COUNT console.log statements!"
exit 1
fiRestore from Backup
Backups are created with .backup extension:
# Restore a single file
mv src/utils/api.ts.backup src/utils/api.ts
# Restore all backups
find . -name "*.backup" -exec sh -c 'mv "$1" "${1%.backup}"' _ {} \;
# Remove all backups (after verifying changes)
find . -name "*.backup" -deleteRequirements
- Node.js 18.0.0 or higher
Support
This project is maintained in my free time. If it saved you from embarrassing console.logs in production, I'd really appreciate your support:
- ⭐ Star the repo—it helps others discover this tool
- 📢 Share with your team or on social media
- 🐛 Report bugs or suggest features
- ☕ Buy me a coffee if you'd like to support development
Thank you to everyone who has contributed, shared feedback, or helped spread the word!
License
MIT
Made with ❤️ for cleaner production code
