Angular2.x – How to disable Console Logs in Production Build
console.log('Disable me in production mode');
When we were developing angular or any front-end application, we probably inserted a lot of console.log statements everywhere in our code for testing purpose. Sometimes we display information that our users are not supposed to see, but we think that we're good because most users will not press F12 (most of the time).
Not only it this a bad practice, we are also being lazy.
I will let explain a trick where we can retain all of our console log statements, when in development mode, and disable all of it when in production mode.
Go to main.ts file, search below lines
if (environment.production) {
enableProdMode();
}
Inside of this IF statement add below line of code
if (window) {
window.console.log = function() {};
}
Your final code suppose to be :
if (environment.production) {
enableProdMode();
if (window) {
window.console.log = function() {};
}
}
Now we have a disabled the console log when we build our app for production environment.
Note: Don't forget to build you application for production environment using below command:
ng build --prod
Thanks for reading !!