Search code examples
reactjsbuildcreate-react-appconsole.log

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?


Solution

  • 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'))