Skip to content

Remove console.log calls from production build #1491

Closed

Description

@edevil

Is there an option to remove all "console.log" calls when building the production artifact?

I know there are some webpack plugins that do this but I'm not sure I can use them without ejecting the project from this framework.

Activity

  1. gaearon commented on Feb 6, 2017

    @gaearon
    Contributor

    No, it’s not supported. You’d need to eject to do this automatically.

  2. lany9527 commented on Sep 29, 2017

    @lany9527
  3. glocore commented on Jan 29, 2018

    @glocore

    I know this is not what this issue is about, but if you're only looking for a way to prevent the console messages from showing up in the production build, but don't mind that the code staying as is (meaning it won't be stripped out of your production code), then you could just monkey-patch console.log() so that it only works in development.

    Or a more elegant way of doing it would be using the debug module, which lets you display messages based on the name of the module calling it. This requires you to add a field to your localStorage as described here. You can simply not add this field for production and the debug messages won't show. I ended up creating a utility class to generate color-coded messages of different levels such as 'trace', 'info', 'warn', 'error', etc. The module name I passed was simply the name of my app, so I just needed to add this in my index.js:

    if(env.process.NODE_ENV !== 'production') localStorage.set('debug', 'name_of_app:*')
    
  4. parkerdan commented on Feb 7, 2018

    @parkerdan

    I have disabled in prod by adding this at the root index.
    Is this a bad approach??

    function noop() {}
    
    if (process.env.NODE_ENV !== 'development') {
      console.log = noop;
      console.warn = noop;
      console.error = noop;
    }
  5. barbagallo commented on Feb 27, 2018

    @barbagallo

    @parkerdan that's not a bad solution tbh, but it also probably depends on what you're doing.

    If someone wanted to be malicious and scan your code for key pieces of data that you use to debug with (i.e. user data, account data..), that'll still leak into the final output and may be found by searching for "console.".

    You could probably obfuscate usage of console by renaming it, and the methods, and then hooking them up to noop functions. Again, it prob depends on what you're trying to protect / hide.

    Or you could use something like this: https://www.npmjs.com/package/webpack-strip

  6. behnammodi commented on Jul 2, 2018

    @behnammodi

    @gaearon Do you plan to add this item?

  7. locked and limited conversation to collaborators on Jan 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions