Home > Software design >  react native console log not showing after install babel-plugin-transform-remove-console
react native console log not showing after install babel-plugin-transform-remove-console

Time:08-11

I installed babel-plugin-transform-remove-console for remove console logs

But its remove console logs forever, What I mean is that I want all console logs to be deleted only in production mode, but after installing this package, even in development mode, all console logs are deleted, and even after uninstalling this package, no console log is still displayed.

This is bable.config.js

module.exports = api => {
  const babelEnv = api.env();
  const plugins = [];
  if (babelEnv !== 'production') {
    plugins.push(['transform-remove-console', {exclude: ['error', 'warn']}]);
  }
  return {
    presets: ['module:metro-react-native-babel-preset'],
    plugins,
  };
};

CodePudding user response:

even in development mode, all console logs are deleted

That's because your if condition is incorrect. It should be if (babelEnv === 'production') {

even after uninstalling this package, no console log is still displayed

Restarting Metro should solve this problem.

Furthermore, babel's environment variable might not be set correctly in react native environment, as discussed in this issue. Based on the solution given there, this is what I have in my app and it works fine.

module.exports = (api) => {
  api.cache(true);

  const plugins = [];

  if (process.env.NODE_ENV === 'production' || process.env.BABEL_ENV === 'production') {
    plugins.push('transform-remove-console');
  }

  plugins.push('react-native-reanimated/plugin');

  return {
    presets: ['module:metro-react-native-babel-preset'],
    plugins,
  };
};
  • Related