Visual Studio Code Can Do That? Debugging with TypeScript
Transcript from the "Debugging with TypeScript" Lesson
>> Burke Holland: Let's move on to something a little more complicated, because often times, you're not running a very simple program like this that's just a single file that has no dependencies and no user interface. This is a rather contrived example, although you can use this. So what I'm gonna do here is I'm gonna move on to our next project here.
[00:00:20] And this project is interesting, because we're using TypeScript. So I'm going to open the index file here and collapse the sidebar. And because actually it's open it again, because I've included a tsconfig here, which instructs TypeScript on what to do, I've given it just some basic instructions. Use the common JS module format, preserve JSX, use Source Maps, in line my Source Maps.
[00:00:48] Because I've given it some basic instructions, VS code will now build this transcript for me. So I'm gonna go ahead and let's delete this file. I should learn the keyboard shortcut for deleting files. Somebody put that in the exercise, submit a pull request. So I've deleted all of the files here and remember, TypeScript doesn't run in the browser or Node.
[00:02:01] So what you can do here is, let's see, we can add our settings. Let's add some settings here. And you'll see that when I do that, it creates a settings.json file. And what I'm gonna do here is I'm gonna come down and just say files, exclude, and it it kind of gives me a list of things that it thinks I probably want to exclude.
[00:02:23] We'll take these out and we'll put in, let's do **/*.js and then we can do, sorry, we have to say, true. And then let's duplicate that, bring it down, and let's say **/*.js.map, is true. Save that, and if you watch over to the side those files are gone.
[00:02:46] Now they're not gone from the file system. They're just gone from the editor. And that just cleans up your editor a whole lot so you can see what you're looking at a little better. You could also use glob expressions up here to do multiple things in one line so you can do this.
[00:03:10] I think it's **/* and then ***. You know what it is, it's like this. Watch this, like this or this.
>> Burke Holland: So any valid glob file, glob expression will work here. If you wanna look up globs, you can just Google that and try to figure out the Syntax.
[00:03:29] But my point is you can create rather complex glob expressions that exclude all kinds of files. All right, so let's go ahead and keep in what we had before.
[00:03:58] Help us to focus, remove this. Go ahead and save that, okay. So now we want to actually debug this application here. So to run this, what we actually need to do is open up a terminal here. So I'm just gonna run this from the terminal so you can see how this runs.
[00:04:15] This program right here is a CLI program, so in other words, it doesn't run in the browser. It runs in Node, but it is activated from the CLI. So it's gonna prompt us for something. We have to input a color then it's gonna change the lamp color. All of that happens in the terminal so if we were to run this, what we would say is, we'd come down here to our terminal.
[00:04:36] We have to make sure we're in the right folder here. So I'm gonna go up one and then move into and actually, I'll show you this. If you just add a new terminal instance, it'll ask you which one of these projects you wanna add? Remember I said terminals are specific to projects?
[00:05:31] What happened, all right, so here's the deal. When you run applications in VS code like I just did, it runs them on an internal console. So that console prompt that we need in order to interact this application, it's there, but you can't see it. So what we need to do is instruct VS code on where to put the output or we need it to use the integrated terminal instead of the internal terminal.
[00:06:01] In order to do that, we are now configuring the debug process so we need to add what's called a launch configuration or a launch config.
>> Burke Holland: What I'm gonna do is I'm gonna come over here to our debug panel and I'm gonna open the drop down list here.
[00:06:25] And you can see here I've got several options for add config, I'm gonna say add config here. And it's gonna wanna know well, what are you trying to debug? We're debugging a node process so we'll choose node, and then it's going to create for us a launch config in the project to folder which it just did.
[00:06:45] So if we come back to our explorer you can see there's now, its collapse some of this, make it a little easier and see this is our launch config. So let's look at this launch config. Right here, you can see that the name of it is launch program that type is node and the request is launch.
[00:07:01] We've two types of launch configs or two types of requests, launch and attach. You can attach a debugger to a running process or you can use the debugger to launch and then it will attach itself. You will almost always use launch, it's much more common to use launch than attach but you can use attach and will look at that here a little bit, it's called launch program.
[00:07:24] Whatever we change this name to is what will show up in the debugger. Here's what I mean by that, right now, if we go back to the right here you see it says launch program. If we change this to Launch CLI app, it changes up here too. So that's what the name does in the launch config.
[00:07:44] The next thing it wants to know is what file is my entry point for running this application. So for us, it tries to guess for you, which is why it took a little bit of time for that launch config to be created. Because VS code is looking at my projects and it's trying to figure out okay, based on the file structure, I think this is what you want.
[00:08:50] And then go ahead and run this from the debugger. So I'm gonna go ahead and launch it. All right, so in the case of this launch config, we actually don't need the out files. This is what I mean by configs being a little bit hard to set up, it's because you really do have to get them just right, and if you don't, the debugging just doesn't work.
[00:09:11] And so I took out the out files, which I'm assuming was providing a bad path and let me go ahead and kill this application so we don't see that anymore. And the debugger should work now so let's go back to the index file here. I'm gonna put a break point and then let's go ahead and launch it.
[00:09:50] So you can see how VS code makes it so much easier to debug. Otherwise you'd have to set all this stuff up yourself, you have to set up Source Maps, that's not a lot of fun. And so VS code does that for you automatically, which is super nice.
[00:10:03] So let's go ahead and continue on through this. Now another thing that you can do is you can set the VS code to auto attach its debugger to your process. So here's what I mean. A lot of times, people are like, cool story about the debugging, but I don't start my apps with the debugger.
[00:10:29] I start them from the terminal with a start command or some sort of specific command that I'm running from MPM or my package JSON and I'd really like to keep it that way. I don't wanna change and if that's the case, what you can do is you can have VS code auto attach to that process whenever it fires up.
[00:10:47] So let me show you how that works. In this case, let's say we were bent on running it like this where we wanted to run it this way. So if we do this, we get the exact same prompt there it goes. Now we want the debugger to attach when we run this application so what we can do is we can toggle on auto attach, okay?
[00:11:08] I'm gonna debug auto attach and then what we have to do is we have to inspect we're saying node and then whenever you wanna use a debugger in node, you have to pass the inspect flag. Because there's a special debugging protocol that it opens when you do that on port 9229.
[00:11:24] You don't need to know any of that information, just the fact that that's the way that it goes. So if we run this with node index.js, nothing's going to happen. What's another color? I'm running out of colors and nothing's gonna happen. But if we run it with node inspect index.js,
>> Burke Holland: Still nothing happens. Let's change it again, there we go. I'll tell you what, let's do it on, I think a better example, this might be on this one here, so I'm going to drop a breakpoint here and let's run into a new terminal instance. I'm going to do node inspect index.js and we want to toggle on auto attach.
[00:12:10] I don't know if I just toggled it on or off, we're about to find out, there we go. Now it's on, I just had to toggle off. So now you can see that the debugger gets hit automatically just by virtue of me starting from the terminal. So just know that if you wanna start from the terminal you can and if you wanna still use the debugger, you can.
[00:12:29] And you need to know about the auto attach feature so that you can auto attach the debugger whenever you run with the --inspect flag. And just remember, if you don't use the inspect flag, nothing is going to happen, because you are not in a debugging protocol for Node.