You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: diagnostics/index.md
+11-49Lines changed: 11 additions & 49 deletions
Original file line number
Diff line number
Diff line change
@@ -5,18 +5,9 @@ author: donovan
5
5
date: 1903-10-09
6
6
---
7
7
8
-
Goals of this lesson:
8
+
In this lesson we will take a look at ways we can diagnose issues in our code. To do this we'll use the built-in Node.js debugger, as well as the Chrome dev tools.
9
9
10
-
* Learn how to set up Node Debugger
11
-
* Step through breakpoints using Repl
12
-
* Inspecting in the Chrome debugger
13
-
* Explore more powerful debugging options
14
-
* Generate diagnostic deports to catch errors
15
-
* Finding memory leaks
16
-
17
-
Pre-requisites:
18
-
19
-
INTRO and expected outcomes
10
+
After this lesson you should be confident investigating errors in a methodical fashion, without relying on `console.log` so much.
20
11
21
12
## Node.js Debugger
22
13
@@ -58,7 +49,7 @@ Wait, I counted too far!
58
49
59
50
Something has gone wrong! Let's use the debugger tool to find out what.
60
51
61
-
### debugger;
52
+
### The `debugger` command
62
53
63
54
In the above code you might have noticed the `debugger;` code on line 3. During normal operation this is ignored, but we can make use of this by running `inspect`:
64
55
@@ -70,7 +61,7 @@ This will enter `debug` mode where we can use command to inspect what is going o
70
61
71
62
Firstly we can use `list()` to take a look at the place in the code we are currently exectuting. At first it'll be on line 16 `startACount()`. All good so far. We can then continue using `cont` to continue exectuting until we reach the break point.
72
63
73
-
By entering the `cont` command we should now see:
64
+
By entering the continue (`cont`) command we should now see:
74
65
75
66
```
76
67
1 function count() {
@@ -113,17 +104,6 @@ So we've managed to get to the moment when the script fails. Enter the `repl` co
113
104
114
105
You can quit the debugger by typing `.exit` or by pressing `Cmd+C` twice.
115
106
116
-
### Watchers
117
-
118
-
119
-
120
-
### Homework
121
-
122
-
Feel free to try moving the `debugger;` statement around in the above. You can also try using the `help` command in the `debug` mode to see other options.
123
-
124
-
Try breaking the code and then using the `debug` mode to narrow down the issue. Inspecting variables using the REPL can be helpful to find logic errors or catch situations where variables contain unexpected values.
125
-
126
-
127
107
## Debugging using Node.js with Chrome DevTools
128
108
129
109
We don't just need the command line to debug our code. We can also make use of Chrome's debugging tools to help navigate our code.
@@ -204,36 +184,18 @@ To create the report, we can run the following command against out `count.js` fi
204
184
node --report-uncaught-exception count.js
205
185
```
206
186
207
-
This generates [a report in JSON](/diagnostics/report.20210505.160745.11097.0.001.json) that you can read to try to work out what has happened.
208
-
187
+
This generates a report in JSON that you can read to try to work out what has happened.
209
188
189
+
It includes a lots of information on the JavaScript and native stack traces, heap statistics, platform information, resource usage and local environmental variables.
210
190
191
+
Along with debugging the error messages from the script, this can be helpful in determining the state of the application when an uncaught exception occurred.
211
192
212
193
With the report option enabled, diagnostic reports can be triggered on unhandled exceptions, fatal errors and user signals, in addition to triggering programmatically through API calls. Read more on the official [diagnostic report docs page](https://nodejs.org/api/report.html).
213
194
195
+
### Homework
214
196
215
-
## Async hooks
216
-
217
-
- A lot of services track everything going on in Node by monkey patching anything that is asynchronous, to keep track of processes over time
218
-
- Introducing AsyncHooks from https://github.com/nodejs/diagnostics
219
-
220
-
* note: don't use console.log inside async_hooks methods as it is asynchronous and will cause infinite `init` calls, instead use `process._rawDebug(msg)`
221
-
222
-
223
-
## Investigating memory leaks
224
-
225
-
// TODO: create example with a memory leak
226
-
227
-
```
228
-
Monitor values from process.memoryUsage() over time.
229
-
```
230
-
231
-
Make heap snapshots
232
-
233
-
https://www.npmjs.com/package/heapdump
234
-
235
-
Compare in chromium/chrome
197
+
Feel free to try moving the `debugger;` statement around in the above. You can also try using the `help` command in the `debug` mode to see other options.
236
198
237
-
// TODO: More on debugging memory leaks
199
+
Try breaking the code and then using the `debug` mode to narrow down the issue. Inspecting variables using the REPL can be helpful to find logic errors or catch situations where variables contain unexpected values.
238
200
239
-
## Summary
201
+
Try out the same exercise using the Chrome devtools and see how it differs. By being familiar with each you will find it easier to know what tools to use to debug any issues in future.
0 commit comments