0

I have used many console.log() in my Express js app. How can I disable all these console.log message by using npm script? For example:"nodemon index.js DEBUG=false"

Steve
  • 21
  • 2
  • 1
    possible duplicate : https://stackoverflow.com/questions/1215392/how-to-quickly-and-conveniently-disable-all-console-log-statements-in-my-code – Mahendra suthar Mar 14 '19 at 03:49
  • Possible duplicate of [How to quickly and conveniently disable all console.log statements in my code?](https://stackoverflow.com/questions/1215392/how-to-quickly-and-conveniently-disable-all-console-log-statements-in-my-code) – Lord Elrond Mar 14 '19 at 05:09

2 Answers2

1

Using console.log() print log messages to the terminal is common practice in development. But these functions are synchronous when the destination is a terminal or a file, so they are not suitable for production.

Instead of using console.log() use a special debugging module like debug.

However, the simple and easy approach is to put this code at the callback function of your app.listen():

// server.js

app.listen(3000, () => {

  if(!process.env.DEBUG){
     console.log = function(){}
  }

 // when DEBUG = false all console.log will not log
  console.log('server started')
});


For more Express.js best practices: performance and reliability checkout their official documentation.

codedawi
  • 314
  • 2
  • 11
0

you can use this plugin which removes all console.* calls.

npm install babel-plugin-transform-remove-console --save

add a .babelrc file with theses configurations

{
  "env": {
    "production": {
      "plugins": ["transform-remove-console"]
    }
  }
}
TRomesh
  • 4,323
  • 8
  • 44
  • 74