Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can remove console.log in the production build of a React application created using create-react-app?

How can remove console.log in the production build of a React application created using create-react-app CRA?

like image 711
Ajay Avatar asked May 23 '19 13:05

Ajay


People also ask

How do I disable console log in production?

if you don't have environment variable then you can jsut simply do. console. log = function () {}; I am using this on my live app to hides the console.

How do I turn off console warnings in React?

The Yellow warning box in React Native can be both helpful and annoying. There are many errors that can be ignored but end up blocking necessary pieces of the application. To disable the yellow box place console. disableYellowBox = true; anywhere in your application.

How do I completely remove create React app?

If you've previously installed create-react-app globally via npm install -g create-react-app , we recommend you uninstall the package using npm uninstall -g create-react-app or yarn global remove create-react-app to ensure that npx always uses the latest version.


1 Answers

I am using this approach to avoid ejecting react-scripts

if (process.env.NODE_ENV === 'production') {
  console.log = () => {}
  console.error = () => {}
  console.debug = () => {}
}

index.js

import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
import './styles/main.scss'

// replace console.* for disable log on production
if (process.env.NODE_ENV === 'production') {
  console.log = () => {}
  console.error = () => {}
  console.debug = () => {}
}

ReactDOM.render(<App />, document.getElementById('root'))
like image 76
Ali Turki Avatar answered Oct 11 '22 09:10

Ali Turki