Need npm tool to strip console statements from JavaScript files automatically

Hey folks! I need to find a way to automatically clean up my JavaScript code by removing all the debug statements like console.log before deploying to production. I have been manually going through files but it’s getting tedious and I keep missing some.

Anyone have experience with npm packages that can handle this task? I’m looking for something that works well with build tools or can run from command line. Would be great if it integrates nicely with existing workflows too.

Appreciate any suggestions you might have!

I ran into this exact problem about six months ago when our QA team kept finding debug logs in production builds. After trying a few different approaches, I settled on using babel-plugin-transform-remove-console which has worked really well for my team.

The setup is straightforward - just add it to your babel config and configure it to run only in production mode. What I like about this approach is that it handles edge cases better than simple regex-based solutions, and it preserves your source maps if you need them for debugging.

One thing to watch out for though is that it will remove ALL console statements by default, including console.error and console.warn which you might actually want to keep in production. You can configure it to be more selective, but make sure to test your build process thoroughly after setting it up.

I’ve been using webpack with the terser plugin for this exact purpose and it works quite well. The terser plugin has a built-in option to drop console statements during the minification process, which means you get both code optimization and console removal in one step.

In your webpack config, you can set the drop_console option to true in the terser configuration. This approach is particularly useful because it integrates seamlessly with most modern build pipelines without requiring additional babel plugins or separate processing steps.

The main advantage I’ve found is that it only removes console statements in production builds automatically, so you don’t need to worry about conditional configuration. However, similar to other solutions, you’ll want to be careful about removing console.error statements that might be important for production error tracking.