How to Run Node.js Apps in VSCode—without Code Runner!

Introduction

I love how Microsoft’s Python and Debugger for Java extensions make testing Python and Java code as easy as pressing a single key (F5, to be more specific). I used to use Code Runner for running my Node.js apps, but I hated how …


This content originally appeared on DEV Community and was authored by Colin Fiedorowicz

Introduction

I love how Microsoft's Python and Debugger for Java extensions make testing Python and Java code as easy as pressing a single key (F5, to be more specific). I used to use 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

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.

  2. 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.

  3. 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.

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

    {
        "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:

  5. Once VSCode creates the launch file, feel free to close it.

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

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

The Integrated Terminal

  1. Add the following line to your launch configuration.

    "console": "integratedTerminal",
    

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

    {
        "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.

  2. Once you add the line to your launch.json file, feel free to close it.

  3. You can run your app by either pressing F5 or clicking the Start Debugging button—just like you would if you were running it in the debugging console.

  4. VSCode will automatically open a new integrated terminal and run your program.

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.


This content originally appeared on DEV Community and was authored by Colin Fiedorowicz


Print Share Comment Cite Upload Translate Updates
APA

Colin Fiedorowicz | Sciencx (2022-06-23T20:06:13+00:00) How to Run Node.js Apps in VSCode—without Code Runner!. Retrieved from https://www.scien.cx/2022/06/23/how-to-run-node-js-apps-in-vscode-without-code-runner/

MLA
" » How to Run Node.js Apps in VSCode—without Code Runner!." Colin Fiedorowicz | Sciencx - Thursday June 23, 2022, https://www.scien.cx/2022/06/23/how-to-run-node-js-apps-in-vscode-without-code-runner/
HARVARD
Colin Fiedorowicz | Sciencx Thursday June 23, 2022 » How to Run Node.js Apps in VSCode—without Code Runner!., viewed ,<https://www.scien.cx/2022/06/23/how-to-run-node-js-apps-in-vscode-without-code-runner/>
VANCOUVER
Colin Fiedorowicz | Sciencx - » How to Run Node.js Apps in VSCode—without Code Runner!. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/06/23/how-to-run-node-js-apps-in-vscode-without-code-runner/
CHICAGO
" » How to Run Node.js Apps in VSCode—without Code Runner!." Colin Fiedorowicz | Sciencx - Accessed . https://www.scien.cx/2022/06/23/how-to-run-node-js-apps-in-vscode-without-code-runner/
IEEE
" » How to Run Node.js Apps in VSCode—without Code Runner!." Colin Fiedorowicz | Sciencx [Online]. Available: https://www.scien.cx/2022/06/23/how-to-run-node-js-apps-in-vscode-without-code-runner/. [Accessed: ]
rf:citation
» How to Run Node.js Apps in VSCode—without Code Runner! | Colin Fiedorowicz | Sciencx | https://www.scien.cx/2022/06/23/how-to-run-node-js-apps-in-vscode-without-code-runner/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.