Can you remove all console.log statements during meteor build?

Is it possible to remove all the console.* statements that are on the client/server when doing a meteor build?

Is there a config option like drop_console in UglifyJS ? I must be overlooking something it seems this should be very easy to remove all the console.* from the code when doing a build (unless you pass --debug).

Do you mean for your running app? If you want to completely disable console.log, you could always monkey patch it. For example:

/client/main.js

console.log('Hi from the console!');

hiddenConsole = console;
console = {
  log() {
    // Do nothing!
  }
};

console.log('Nothing happens!');
hiddenConsole.log('Hi from the hidden console!');

You could have a flag set via your Meteor.settings to enable/disable the console.log, which you could then adjust for different environments. This will work, but it’s a hack for sure. A better option would be to look into using one of the many existing javascript logging libraries that let you enable/disable logging by environment, set logging levels, fire logging details to 3rd party services, etc. (thereby replacing the need for console.log). Winston is a really popular one, but I also like loglevel.

1 Like

Meteor does use Uglify for minification but I don’t think there’s any config exposed. You may be able to add drop_console here in a local package replacement for the standard-minifier-js package

Or you could use a helper:

if (process.env.NODE_ENV) {
  devLog = console.log.bind(console);
} else {
  devLog = function () {};
}

devLog('Something happened!');
1 Like