31
loading...
This website collects cookies to deliver better user experience
console.log
ing to debug code.const importantNumbers = "1123, 435, 8712, 843"
let res = 0;
importantNumbers.split(', ').forEach(number => res += number);
console.log(res);
// => NaN
// Why?
node --inspect file.js
. This opens a WebSocket connection to something like ws://127.0.0.1:9229/uuid-here
. Now, clients can connect to this debugger and debug code. You can even build your own clients!node inspect file.js
(notice it's a command, not an option now). You can install the latest standalone version of this command by installing node-inspect.node inspect file.js
, you should get some output like this:$ node inspect file.js
< Debugger listening on ws://127.0.0.1:9229/d7d8aec2-819b-411a-abdd-900b6b90dbfc
< For help, see: https://nodejs.org/en/docs/inspector
< Debugger attached.
Break on start in file.js:1
> 1 const importantNumbers = "1123, 435, 8712, 843"
2 let res = 0;
3
debug>
cont
, c
: Continue executionnext
, n
: Step nextstep
, s
: Step inout
, o
: Step outpause
: Pause running code (like pause button in Developer Tools)n
and hitting enter. We'll then get output like this:debug> n
break in file.js:2
1 const importantNumbers = "1123, 435, 8712, 843"
> 2 let res = 0;
3
4 importantNumbers.split(', ').forEach(number => res += number);
debug>
0
has been highlighted as it is the next step.debug> n
break in resources/debugging.js:4
2 let res = 0;
3
> 4 importantNumbers.split(', ').forEach(number => res += number);
5
6 console.log(res);
debug>
watch
our important variables:debug> watch('number')
debug> watch('res')
debug>
step
instead of going to the next line. So type s
and hit enter, and you'll get this:debug> s
break in file.js:4
Watchers:
0: number = '1123'
1: res = 0
2 let res = 0;
3
> 4 importantNumbers.split(', ').forEach(number => res += number);
5
6 console.log(res);
debug>
res
is 0
and number
is '1123'
. We can easily understand that number + string = NaN
.