I am using MEAN, Angular 2, Node/Express, Angular-CLI and ng build --prod to build my app and I have a travesty of commented out throwaway code and a billion debugging console.log statements throughout my app. Is there a way to have the build process remove all comments and console.log statements when it builds? The thought of doing it manually is scary!
-
I'd imagine the comments are already removed, have you looked at the output? But how is it supposed to know which logs are functionality and which just debugging? – jonrsharpe Feb 17 '17 at 21:05
-
angular-cli uses webpack under the hood, which is already doing some kind of mangling of the javascript during the bundling process. I believe there's plugins for webpack to remove console logging and also comments during the mangle process, but I'm not sure how to sync that up with angular-cli. – disappointed in SO leadership Feb 17 '17 at 21:13
-
1You can use Gulp with gulp-strip-debug. Check out [this article](https://medium.com/@jun711.g/how-to-remove-console-log-from-your-javascript-files-programmatically-469eed2ec8fe) for more information. – Jun May 05 '19 at 02:55
-
1please remove commented blocks and console.log statements from the CODE to save future developers their sanity. This is how wars get started. – java-addict301 Feb 18 '20 at 20:03
6 Answers
I have simple fix. Put this code in main.ts
if(env === 'prod') { // assuming you have env variable configured
// check if window exists, if you render backend window will not be available
if(window){
window.console.log = function(){};
}
}

- 2,066
- 2
- 23
- 47

- 2,904
- 8
- 33
- 47
-
3lol nice idea, problem is I have more console.log statements in my server-side code :P thanks though – seanEd Feb 18 '17 at 15:44
-
3You can do the same on server code too. Put that code in `server.js`. Don't attach to window though because it does not exist in node env. – Sudhakar Feb 18 '17 at 19:09
-
-
I have used it in my main.ts, but this is not working! My webpack version is 3.5.5, does it have something to do with webpack version? – Pramod_Para Sep 30 '17 at 05:45
-
Any update on this? Is this still a viable solution? Also, how exactly does this work? It simply intercepts all console logs and so that prevents anything from being printed in the console? – Muirik Oct 27 '17 at 21:48
-
Yes, its simply overriding the prototype method with an empty function. – Sudhakar Oct 27 '17 at 21:56
-
This worked for me! I added this in my main.ts as below: if (environment.production) { enableProdMode(); if(window){ window.console.log = function(){}; } } – marty331 Jan 21 '19 at 13:26
-
18Surprised this is the accepted solution here. This doesn't strip console.log. It simply changes `console.log` to do nothing. That will impact any javascript running in the current window, not just your own. This is a terrible practice. A better solution would be for webpack to strip the console.log calls from the files during the bundling/minification process. – crush Feb 07 '19 at 18:25
-
I wouldn't recommend this bc the browser stills executes the placeholder function, which is still CPU effort for the users device. See: https://www.codementor.io/brijmcq/angular-clear-all-of-your-console-logs-in-production-build-with-just-a-few-lines-of-code-cxcw30yqs#comment-sjclji9d8 – Lea Reimann Apr 26 '19 at 07:57
Simply add this window.console.log = function(){};
to `
if (environment.production) {
enableProdMode();
}`

- 13,162
- 13
- 64
- 86

- 466
- 5
- 10
-
3I wouldn't recommend this bc the browser stills executes the placeholder function, which is still CPU effort for the users device. See: https://www.codementor.io/brijmcq/angular-clear-all-of-your-console-logs-in-production-build-with-just-a-few-lines-of-code-cxcw30yqs#comment-sjclji9d8 – Lea Reimann Apr 26 '19 at 08:02
You can use ng lint
with --fix
flag and no-console
rule in tslint configuration file. And hook it to your build call in your package file.
eg.:
...
"prebuild": "ng lint --fix",
"build": "ng build -prod",
...
and build the app
npm run build

- 320
- 2
- 6
-
1
-
2Yes, but the `prebuild ` commend won't trigger. You can make two calls though: `ng lint --fix && ng build -prod` – Jan May 05 '17 at 15:20
-
I like the idea, but running lint with --fix option doesn't actually remove the console uses in my files. Any idea why? – Bob Jun 17 '17 at 03:23
-
2My bad... `no-console` doesn't have a fixer https://palantir.github.io/tslint/rules/ – Jan Jun 20 '17 at 07:19
-
3An other option would be to use webpack. You can generate the config by hitting: `ng eject` And the use the loader `webpack-strip` https://www.npmjs.com/package/webpack-strip Hope it helps! – Jan Jun 20 '17 at 07:43
-
4Careful with `ng eject` as you will then be forced to manage webpack yourself going forward, and is not easy to reverse this process. – crush Feb 07 '19 at 18:26
-
This is an old thread but I'd 100% advise against `ng eject` - I've worked on projects with custom webpack and cli and going back to cli after ejection is a total PAIN - not to mention configuring/updating without the cli is a slow process. – Levidps Apr 26 '21 at 03:10
Another not completely explained option is (if linting or custom webpack isn't the way to go):
To strip the log statements from the code by your build.
So, here is what I have done (I use nx on top of Angular CLI):
- Adding a custom script that cleans all console.log calls
utils/clean-console.log.js
const glob = require('glob');
const replace = require('replace');
// list all directories you want to scan
// (I have a nx mono.repo with multiple involved projects & libs)
[
'./apps/<app>/src/**/*.ts',
'./libs/*/src/**/*.ts',
].forEach(pattern => glob(pattern, (err, files) => {
if (err) {
throw err;
}
files.forEach(item => replace({
// change to your needs which levels you want to strip
// the levels listed are removed from code
regex: /.*console\.(log|info|debug|warn)\([^\)]*\);?.*/gm,
replacement: '',
paths: [item],
}));
}));
- Updating
package.json
for my build command
node utils/clean-console.log.js && nx run <app>:build:prod
This works for me pretty nicely (even for multiline log statements) since I have a consistent code style. Every log statement is followed by a new line. If you don't have that, you can't use the script right ahead.
Btw. you change the code with this. But for me in pipeline context it doesn't matter since every build will be checked out again & what happens to the code inside the pipeline doesn't matter. Only the compiled output does matter...

- 151
- 9
Don't forget to do the same for the other consoles
if (environment.production) {
enableProdMode();
if(window){
window.console.log = function(){};
window.console.warn = function(){};
window.console.error = function(){};
}
}

- 544
- 7
- 14
You can hide all the console.log()
in production (angular) by two ways.
They are
- hiding throughout the angular app
- hinding them in particular component only.
- Hiding throughout the app
open main.ts
file and add the following lines inside production check condition
if (environment.production) {
enableProdMode();
if (window) {
window.console.log = function() {};
}
}
or
if (environment.production) {
enableProdMode();
window.console.log = function() {};
}
- Hiding in particular component only
Open that component's component.ts file and add the following code inside ngOnInit life cycle hook
import { isDevMode } from '@angular/core';
....
ngOnInit() {
if (!isDevMode()) {
console.log = function() {};
}
}
This will hide them only in that particular component.
If you wanna hide them in IE, please check this
if you get linting error for not using normal function, replace it with the following arrow function
console.log = () => {};

- 2,676
- 1
- 30
- 35