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.
2 Answers
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.
3 Comments
Sagar Acharya
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.
Sagar Acharya
Is there any way for me to remove console without having to build it? I need to remove them based on devlopment environments
codejockie
You can convert your
.babelrc to babel.config.js and then set the plugin based on NODE_ENVIn 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
Sagar Acharya
I actually needed a solution for react not react-native.
Yossi
I see... No charge, then ;)