How to Run Node.js Apps in VSCode—without Code Runner!
Tags: codenewbie, tutorial, node, vscode
Published: 2022-06-23 20:06:13

## Introduction

I love how Microsoft's _[Python](https://marketplace.visualstudio.com/items?itemName=ms-python.python)_ and _[Debugger for Java](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-debug)_ extensions make testing Python and Java code as easy as pressing a single key (<kbd>F5</kbd>, to be more specific). I used to use [Code Runner](https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner) for running my Node.js apps, but I hated how it added an extra context menu item for running Java and Python code. Since the Microsoft extensions already add their own context menu items, I felt that Code Runner's addition was redundant. Eventually, the extra item annoyed me so much that I uninstalled Code Runner and began using the command line to run my Node.js apps.

![The Code Runner extension adds an extra context menu item for running Java code, even though the Debugger for Java extension already adds one](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aujlxlrg96vinbf9jozb.png)

Little did I know that there was an easier way in front of me the entire time. In fact, it comes with every copy of VSCode! It turns out that VSCode comes with a built-in extension for running and debugging both Node.js _and_ browser-based JavaScript. That's right, it was literally _in front_ of me all along!

In this article, I'll teach you how to use VSCode's built-in JavaScript debugger to run your Node.js apps.

## Okay, So How Do You Run Them?

VSCode has two places where you can run your Node.js apps: the debugging console and the integrated terminal. There's an important reason why you might need to use the integrated terminal instead of the debugging console. While it suffices for most situations, the debugging console can't take user input. Thus, you might wish to use the integrated terminal to run most (if not all) of your apps.

This tutorial has two parts. The first part teaches you how to run your Node.js apps in the debugging console, and the second part teaches you how to run them in the integrated terminal. Even if you never plan on using the debugging console, you should still read the first part of the tutorial because the second part builds off of it.

### The Debugging Console

1.  In VSCode, open the directory containing your app. Then navigate to the _Run and Debug_ view in the activity bar.

    <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/out01l1kasl1qmy9cd7f.png" alt="">

1.  In the Run and Debug view, click on the text that says "create a launch.json file." Launch files contain special settings that tell VSCode how to run files in the debugger.

    <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q4v2kqqcvxlejb2g38ky.png" alt="">

1.  Once you click on the text, you'll be prompted to select a debugger. Since you'll be running a Node.js app, you should choose `Node.js`.

    <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lmlfk8o0ih0elmyso2a8.png" alt="">

1.  Once you select the Node.js debugger, it'll automatically create and open a `launch.json` file which will have the following properties:

    ```json
    {
        "version": "0.2.0",
        "configurations": [
            {
                "type": "pwa-node",
                "request": "launch",
                "name": "Launch Program",
                "skipFiles": [
                    "<node_internals>/**"
                ],
                "program": "${workspaceFolder}/app.js"
            }
        ]
    }
    ```

    The object in the array for the `"configurations"` property contains the most relevant properties, so I'll explain each of its keys and acceptable values:
    -   The `type` key stores which debugger VSCode should use. In this case, it's set to `"pwa-node"`—the Node.js debugger. You can also set it to either `"pwa-chrome"` or `"pwa-msedge"`, allowing you to run web apps in Google Chrome and Microsoft Edge, respectively. It's important to mention that these aren't the only debuggers you can use; you can [install additional debuggers from the VSCode Marketplace](https://marketplace.visualstudio.com/search?target=VSCode&category=Debuggers).
    -   The `request` key can store one of two values: `"launch"` and `"attach"`. In this case, it's set to the former, meaning VSCode will attach the debugger to a new instance of your app. When set to the latter, VSCode will attach the debugger to an app or process that's already running.
    -   The `name` key is self-explanatory: it stores the configuration's name. The ability to name them comes in handy when you must run multiple debuggers on a single project. Configuration names appear in the Run and Debug view when selecting one to run. In this case, the configuration is named "Launch Program," but there's nothing wrong with giving it a different name like "Run Node.js App."
    -   The `skipFiles` key stores an array of glob patterns indicating files that the debugger should skip. In this case, it's value tells the VSCode debugger to skip internal Node.js files
    -   The `program` key stores the absolute path of you're Node.js app. It's important to mention that VSCode has [various predefined variables](https://code.visualstudio.com/docs/editor/variables-reference#_predefined-variables) which store file paths that most programmers regularly use. In this case, the path uses the `${workspaceFolder}` variable, which stores the workspace's directory.
1.  Once VSCode creates the launch file, feel free to close it.

    <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9yurgwrlct8qb83jyoip.png" alt="">

1.  Now you're ready to run your Node.js app! You can run it by either pressing <kbd>F5</kbd> or clicking the green arrow button in the Run and Debug view.

    <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jisd55auohyzpfcghq56.png" alt="">

1.  VSCode will automatically open the debug console and run your program.

    <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3r2vzx97v6prvlfb93yk.png" alt="">

### The Integrated Terminal

1.  Add the following line to your launch configuration.

    ```json
    "console": "integratedTerminal",
    ```

    Once you do, you're `launch.json` file should look like this:

    ```json
    {
        "version": "0.2.0",
        "configurations": [
            {
                "type": "pwa-node",
                "request": "launch",
                "name": "Launch Program",
                "skipFiles": [
                    "<node_internals>/**"
                ],
                "program": "${workspaceFolder}/app.js",
                "console": "integratedTerminal",
            }
        ]
    }
    ```

    The `console` key stores which console VSCode should use for debugging. It uses the debugging console by default, but we've now set it to use the integrated terminal.
1.  Once you add the line to your `launch.json` file, feel free to close it.
1.  You can run your app by either pressing <kbd>F5</kbd> or clicking the Start Debugging button—just like you would if you were running it in the debugging console.
1.  VSCode will automatically open a new integrated terminal and run your program.

    <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/009ehk91e4fyte03rae5.png" alt="">

__That's that; you now know how to run Node.js apps in VSCode! If you want to learn more about debugging Node.js apps in VSCode, then visit [the VSCode documentation website](https://code.visualstudio.com/docs/nodejs/nodejs-debugging).__