2

I don't want to show the warnings in the console for a certain development environment. Is there any way to achieve that? My application was bootstrapped using create react app.

1
  • 3
    You don't disable them, you fix them. Commented Apr 16, 2019 at 5:31

2 Answers 2

2

Since you are using React, my guess is you are already using babel. There's a plugin for that purpose. It's called babel-plugin-transform-remove-console. This will exclude all console.log's statement during the build process.
Install that in your app and configure it via .babelrc as follows:

{
  "plugins": ["transform-remove-console"]
}

You can also specify the variant(s) of the console functions to exclude:

{
  "plugins": [ ["transform-remove-console", { "exclude": [ "error", "warn"] }] ]
}

My advise is to not use console logs in your code except necessary.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the response. The logs are generated by proptypes and i actually dont have time currently to fix them all so i wanted to disable them for my production build currently.
Is there any way for me to remove console without having to build it? I need to remove them based on devlopment environments
You can convert your .babelrc to babel.config.js and then set the plugin based on NODE_ENV
1

In my App.js I have the following code for accomplishing this:

import { YellowBox } from 'react-native';

componentDidMount() {
    // The following lines are a workaround
    // in order to stop getting warnings about timer
    // See: https://github.com/firebase/firebase-js-sdk/issues/97#issuecomment-365456531
    YellowBox.ignoreWarnings(['Setting a timer']);
    const _console = _.clone(console);
    console.warn = message => {
      if (message.indexOf('Setting a timer') <= -1) {
        _console.warn(message);
      }
    };
}

2 Comments

I actually needed a solution for react not react-native.
I see... No charge, then ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.