How can remove console.log in the production build of a React application created using create-react-app CRA?
The simple way is to create a new file in root named logger.js
In logger.js
var logger = function logger(...messages){
if(process.env.NODE_ENV === 'development'){
console.log(messages);
}
}
exports.logger = logger;
Now import this logger.js file to your components. For example my component file is demo.jsx
import React from 'react';
var logger = require('../logger.js').logger;
class Demo extends React.Component{
constructor(props){
}
render(){
return(
Hello i am demo component
);
}
componentDidMount(){
logger('I am printing only in developmet')
}
}
export default Demo;