Angular  – Clear all browser console messages in PROD mode

Reading Time: < 1 minute

Have you ever come across a situation where your sensitive information is displaying into your browser console? And, you thought of removing them before pushing your code to your source control but, forgot to do that?

We usually see lots of console messages are being logged into the browser console. These console messages help us greatly in the development environment. But, in production, it is not recommended.

As a developer, we heavily use console messages to debug client applications. It helps us to troubleshoot the issues and statuses of functions.

As developers use these console messages very heavily. It is very difficult to navigate to each console message and remove it manually.

Here is the simple trick to remove all the console messages from the angular application. You can enable this in only PROD mode and disable in development mode.

Please navigate to your main.ts file and paste below code

if (environment.production) {
   enableProdMode();
   if(window){
      window.console.log=function(){};
  }
}

Now, you can verify it by running your angular application in PROD mode and verify the results.

0 0 vote
Article Rating