Language: EN

configurar-vscode-para-nodejs

How to work with Visual Studio Code and Node.js

Let’s see how to set up Visual Studio Code (VSCode) to work with Node.js. It is likely that VSCode is the tool you most frequently use with Node.js.

When working on a programming project, it is not common to use a simple text editor, but rather an IDE. An IDE is a development environment, it is a “hyper-vitaminized text editor” that incorporates more tools.

One of the most popular IDEs is the well-known VSCode. And we are in luck!, because they fit together perfectly, and are great work companions.

Installing Visual Studio Code

Before we start, make sure you have Visual Studio Code installed on your system. If you don’t already know what that is, or don’t have it installed, check out this post

Creating a Node.js script in VSCode

Creating a script in Node.js with Visual Studio Code is not very difficult. We simply open VSCode in any folder where we want to work.

Now, we right-click / create a new item, and create our file. For example hello_world.js.

nodejs-vscode-files

Well, I had already told you it was going to be very easy. Of course you can create the file and folder structure you want. Or, you can use the integrated terminal in VSCode.

Running the File

Now comes the good part. To run our program we can use the integrated terminal, and run the following command,

node hello_world.js

You should see the output “Hello World from Node.js!” in the terminal. This is basically the same as what we did in the previous tutorial.

Oooooo,… now… you can also press F5 (which is the same as going to the top menu and choosing Run / Star Debugging).

You’ll see a window like the following, where nodejs-vscode-run where VSCode is basically asking you “how do you want to run this”. We select “Node.js” from the dropdown.

VSCode will take the file you have open and run it in Node.js. It will also show you the result in the integrated “Debug Console” window.

nodejs-vscode-debug-console

That is, you don’t have to use the command prompt to run node whatever.js. You just have to press F5. If you like to type, then use the command prompt… I press F5. 😊

Debugging Node.js Applications in VSCode

Did you like it? Well, the best is yet to come. VSCode provides debugging tools that can help you identify and fix errors in your Node.js code.

Let’s create a breakpoint. In your hello_world.js file, click on the left margin next to the line of code where you want to set a breakpoint. This will create a red dot, indicating a breakpoint.

nodejs-vscode-breakpoint

Now press F5 or select Run > Start Debugging to start the debugging. Execution will stop at the breakpoints you have set.

nodejs-vscode-debug

While you are in debug mode, you can inspect the value of variables. Just hover over a variable to see its current value.

The debugging console in VSCode allows you to run JavaScript commands while in debug mode. You can view the value of variables, run functions, and more.

Now, you have what you need to start working properly with Node.js and Visual Studio Code.