console.log() from node_modules

artooras picture artooras · Dec 10, 2018 · Viewed 8.4k times · Source

How do I console.log() from dependencies in node_modules in my node project? I'm using the default create-react-app setup.

When I include console.log() in my application code, logging works fine. However, when I try to include console.log() in the code of one of the project's dependencies in node_modules, these do not appear in my console.

Any way I can get those logs?

Answer

Russ Brown picture Russ Brown · Dec 10, 2018

If you are monkey patching an npm module with a console.log() to debug an issue it should show up just like any other console statement would. It's probable that your root cause is your build. I'm making some assumptions that you are using babel and a bundler tool like Webpack.

  • Make sure you are doing a full rebuild of your project
  • clear babel cache or try BABEL_DISABLE_CACHE=1 webpack
  • Double check the console.log you are adding isn't in source code of the dependency therefor is never being called.
  • Try adding a console.log higher up in the file of dependency to better know it's being loaded at all

Alternatively I'd personally recommend you reconsider your approach. While I've actually done this a couple times; if you are adding "debugging" like this to lower level modules you are probably looking in the wrong place for your issue unless there is a legit bug in the lib...