Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing log statements in Create React App without ejecting

I use lots of console.log() statements while developing Apps (and I mean LOTS). How exactly can I remove them without "ejecting" from a Create React App What I considered but not sure how exactly to implement:

In my package.json:

 "build": "react-scripts build && uglifyjs --compress drop_console build/static/js/main.xxxxx.js  -o build/static/js/main.xxxxx.js

However How exactly can I know the hash suffix on the main.js file so I can call this command and save the output js file with same filename

like image 528
jasan Avatar asked Dec 15 '17 20:12

jasan


People also ask

Should I eject from Create React app?

Create React App is the easiest and most available way to get into React. And ejecting your app is seen as the gateway to being a “Real React Developer.” I'm not normally for gatekeeping, but ejecting is worth avoiding.

How do I remove warnings in Reactjs?

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.

What to remove from 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.

How do I get rid of console log in React Native?

For debugging purposes, I often use console. log statements in React Native and Expo applications. A babel plugin called babel-plugin-transform-remove-console takes care of removing any console statements from the code.


1 Answers

Add this to index.js

if (process.env.NODE_ENV !== 'development') {
  console.log = () => {}
}

Note that this will only suppress the messages, it will not strip them from your deployed code.

Note Nov 2020

Don't do this for library modules, it will disable console.log for the parent project.

Update Sept 2020

There is some movement on this issue on the CRA repo... go give it support/thumbs up here https://github.com/facebook/create-react-app/pull/9222

References: How to quickly and conveniently disable all console.log statements in my code?

like image 13
Simon Hutchison Avatar answered Oct 23 '22 12:10

Simon Hutchison