Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 826
Carol.Kar Avatar asked Nov 07 '17 21:11

Carol.Kar


2 Answers

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"
     }
    
like image 190
kgangadhar Avatar answered Sep 27 '22 20:09

kgangadhar


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}"
    }
  ]
}
like image 33
Maksim Shamihulau Avatar answered Sep 27 '22 21:09

Maksim Shamihulau