One Command to Rule Them All: Instant Console.Log Cleanup!

Caner Kuru
3 min readMay 23, 2023

Debugging and error handling are essential skills that every developer must master. No application can genuinely be deemed complete without robust error handling in place. Among various tools available for debugging and error handling in frontend development, console.log commands are perhaps the most frequently used. There have been instances where I’ve found myself deploying changes with more than a dozen console.log statements still present in my code, not to mention many others that were commented out. This can cause unnecessary clutter and potentially reveal sensitive data.

To solve this issue, I’ve created a function named removeConsoleLogs. This automated solution ensures that all console.log statements are meticulously removed from the codebase before deployment, providing cleaner and more secure code. Let’s delve into how it works.

How Does It Work?

  • I created an array called ignoredDirectories, where you can list down the directories you want the script to ignore.
  • /((\/\/)?\s*console\.log\(([^)]|\n)*\);?)/g: This regex is checking for any instance of console.log() in the string, including commented ones (using //).
  • The script will check each .js, .jsx, .ts, and .tsx file it comes across.

--

--

Caner Kuru

React.js & React Native Guru | TanStack/React Query Lover | Software Engineer | Gamer