Carol.Kar
Carol.Kar

Reputation: 5355

Debug single javascript file in “Visual Studio Code”

Is there a way to debug a single javascript file step by step without launching a node server?

For example seed files by knex.

Node is definitely needed, but I do not know how to start the VSC debugger with only the file.

Upvotes: 26

Views: 21412

Answers (5)

Maksim Shamihulau
Maksim Shamihulau

Reputation: 1698

To launch(debug) currently opened/active *.js file, you should have the following configuration in your launch.json file:

{
  "version": "0.2.0",
  "configurations": [
    {
        "type": "node",
        "request": "launch",
        "name": "Launch Current Opened File",
        "program": "${file}"
    }
  ]
}

Upvotes: 14

kgangadhar
kgangadhar

Reputation: 5088

There are two ways to achieve this:

  • Just add launch.json and give your file_name. and start debugging.

    For example, If your file_name is index.js. create a folder called .vscode and inside this folder create launch.json, structure looks like this:

                  main_folder
                       |___ index.js
                       |___ .vscode
                               |___ launch.json
    

    and provide path as below in launch.json:

     {
         "version": "0.2.0",
           "configurations": [
             {
               "type": "node",
               "request": "launch",
               "name": "Launch Current Opened File",
               "program": "${file}"
             }
           ]
      }
    
  • The second option is to create a package.json and give your file an entry point. when you press F5, vscode will consider this file as starting point.

                     main_folder
                         |___ index.js
                         |___ package.json
    

    you can create package.json manually or can create it using npm init, This will ask you a bunch of questions, and then write a package.json for you.

     {
       "name": "application_name",
       "version": "0.0.0",
       "description": "for single page debugging",
       "main": "index.js",
       "author": "",
       "license": "ISC"
     }
    

Upvotes: 34

Josh Weston
Josh Weston

Reputation: 1890

To help with any confusion, your debug options depend on how your workspace is setup:

  1. If you have not created a launch.json file, you will see the following options in the debug panel. Clicking Run and Debug will debug the currently active file.

    enter image description here

  2. If you have a package.json file, you will still see the same view shown above; however, VSCode will first try to debug the file name you have specified in the main attribute of your package.json. If it does not find that file, it will then debug the currently active file. So, for instance, if my package.json shows index.js as my main file, then VSCode will always run that file in the debugger if it can find it instead of your currently active file.

  3. Finally, you can be more explicit by adding configurations to launch.json. When you do this you can then choose which file to debug from the drop-down. In my environment, I add an option to be able to run the currently active file (the first entry in the JSON below), as well as, any other files I want to access quickly (the second entry in the JSON below). Now, the dropdown will show these options to choose from.

    {
        // Use IntelliSense to learn about possible attributes.
        // Hover to view descriptions of existing attributes.
        // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [
            {
                "type": "node",
                "request": "launch",
                "name": "Debug this file",
                "program": "${file}",
                "skipFiles": [
                    "<node_internals>/**"
                ]
            },
            {
                "type": "node",
                "request": "launch",
                "name": "Debug testing.js",
                "program": "${workspaceFolder}/testing.js",
                "skipFiles": [
                    "<node_internals>/**"
                ]
            }
        ]
    }
    

    enter image description here

For more details, check-out Debugging in Visual Studio Code.

Upvotes: 2

Rob McFeely
Rob McFeely

Reputation: 3033

The easiest way for me... Right-click on the file in VS file explorer.
Click "open in Terminal".
Then in terminal type node myFile.js

Upvotes: -4

samlandfried
samlandfried

Reputation: 821

You can run your current file in a Node environment without creating a launch.json.

With the file you want to debug open, go to the debugger panel, click the green arrow and choose Node as your environment.

From the folks at VS Code.

Upvotes: 5

Related Questions