Debug single javascript file in “Visual Studio Code”

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.


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}"
    }
  ]
}

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"
     }