Debugging Node.js
There are 2 kinds of bugs:
Bugs that throw explicit errors that you can read
Bugs caused by some problem in the code's logic, which throws no errors
Here are some techniques for debugging:
console.log
This is a fundamental way to uncover the values of things, making it clearer where things go wrong.
debugger
,node inspect
, and Chrome dev toolsAdd
debugger
at the specific line where you want the code to stop. The point at which you stop gives you access to its execution context.Run
node inspect <commands>
to spin up a live server.Access at
chrome://inspect
and click inspect. Chrome dev tools will pop open.Using Sources, click Run. The code will stop at the
debugger
line. You can see everything going on. You can even use the Console to mess around with the available variables.If you want to run the debugger again, just type
restart
in the terminal.
Pro tip: You might notice in Chrome dev tools that your code is wrapped inside a function. The function wrapper is automatically added by Node.js to inject access to core modules and environment variables.
Last updated