0

In my HTML I load a Javascript module like this:

<script type="text/javascript" src="static/js/mymodule.js"></script>

But mymodule.js contains console.log commands inside so I can see their outputs every time when I reload the page. How can I prevent it?

Of course, I could remove all console.log calls, but what if I deal with external library that I can't modify? Maybe there's a way to prevent logging in a particular script-tag.

Fomalhaut
  • 8,590
  • 8
  • 51
  • 95

3 Answers3

0

I don't think there is a way to do that (I hope I'm wrong). If an external library has console.logs in their production code, you should consider using an alternative one.

Because you have control of the file where the console.log exists, you should delete it or comment it out.

AdamHinckley
  • 215
  • 4
  • 16
0

try this :

console.log = function () {}
glinda93
  • 7,659
  • 5
  • 40
  • 78
Yasa12
  • 11
  • 1
    Please try to explain your answer. – mightyWOZ Mar 01 '20 at 06:29
  • I think this answer is the most straightforward and working one. However, I won't upvote it because it feels somewhat guilty to overwrite javascript internal functions. If you want some quick dirty hacks, yes it will do the magic for you. And I wonder if there's any other way to suppress all console logs. – glinda93 Mar 01 '20 at 07:43
  • This will disable **all** console logs, not a good solution. – MkMan Apr 26 '22 at 23:22
0

You can do that by: stripping out the console.log statements using build tool settings. Enable the build tool settings to strip console.log for production build whereas you can still keep them for development build.

Most of the build tools have support for this. e.g webpack, gulp etc.

halfer
  • 19,824
  • 17
  • 99
  • 186
Harish Dhami
  • 1,016
  • 1
  • 7
  • 10