Skip to content

Commit

Permalink
none
Browse files Browse the repository at this point in the history
  • Loading branch information
freddieventura committed May 11, 2024
1 parent 062cb06 commit d9f56b1
Showing 1 changed file with 79 additions and 2 deletions.
81 changes: 79 additions & 2 deletions _posts/2024-05-11-js-intermediate-13-nodejs-debugging-cli.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,90 @@
---
title: Javascript Intermediate 0
title: Javascript Intermediate 13 - Nodejs debugging on the CLI
tags: javascript, programming, coding, nodejs, syntax
article_header:
type: cover
image:
src: images/js-intermediate-dog-02.png
---

# Title

There may be thousands of libraries and utilities that allow you debug **nodejs** projects, but I am going to write few lines on how to do it using the node built-in tool.
Is not nearly as powerful as **gdb** , but using the right strategy to approach the process can clarifying as well as timesaving, as you wont be needing to install some other additional tools.


You can start debugging your proyect via the entry file by
```
node inspect <myfile.js>
help
```

You get the commands over there but mainly we will be focusing on the state of the variables as the code statements are been executed.


First set a breakpoint, someline on your code so you can iterate from there

```
# set breakpoint
sb(<lineNum>)
```

Then you want to basically use one of these three commands

```
next, n Continue to next line in current file
step, s Step into, potentially entering a function
cont, c Resume execution
```

Continue to iterate to the next time you get to the following break point
Next and step if you want to execute one line at a time.
Note that step implies check further into function calls.
If using Step within buil-in functions and libraries, it will take you to lines of code outside the codebase of the project.
Just bear that in mind as it may confuse you.

At any prompt point on the debugger (when it is paused) , you can issue commands such as.

```
#exec('<codeToExecute>'
exec('console.log(i)')
```

But what you really want to do, is to watch variables. They will get printed out at each Step.

```
watch('<varName>')
```

Normaly you want to watch the state many variables at the same time, for that you would need like various watch commands.
What I personally use (because I am going to be repeating the same steps backs and forths) is to write a one-liner to activate such as watch command.
So at the end of my .js file I write something like

```
/*
sb(20)
watch('varA');watch('varB');watch('varC');watch('varD');watch('varE');
*/
```

So I remember, the commands I have to do and just focus on reading the output of the debugger instead.

Personally it is a shame that you cannot set a breakpoint somewhere on your code in a deterministic way.
Say you want to target a function entry point (a function call) you check the lineNumber where that happens and do `sb(23)` , but say you modify some lines previous that line , then that `sb(23)` is no longer pointing to the same statement.

If you can and want modify the codeIn node.js you have the following statement which will tell the debugger to create a breakpoint there
```
debugger; // DEBUGGER BREAKPOINT
```

But if not , note that breakpoint lineNo


Finally to exit the debugger

```
.exit
```


## Javascript Intermediate tutorials

Expand Down

0 comments on commit d9f56b1

Please sign in to comment.