Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging current file in VS Code

I am writing javascript and am currently doing simple exercises/programs. At times, I wish to run my file for testing purposes. I am aware I could create an HTML file and do this within the console. In Sublime, there exists a way to "build" the current file and immediately see the results (say, whatever is sent to console.log).

With VS Code, it seems that for every file I want to "build"/debug in this manner, I must manually change the launch.json file to reflect the name of the current program.

I have been researching a way around this, and I learned that there are variables like ${file} , but when I use that in the launch.json "program" attribute, for example:

"program": "${workspaceRoot}/${file}"

with or without the workspaceRoot part, I get the following error:

Attribute "program" does not exist" (file name here). 

Am I missing a simple way to accomplish this, or must I keep editing launch.json every time I want to run the file?

Thanks in advance!

like image 827
jdb79 Avatar asked Jul 17 '16 09:07

jdb79


People also ask

How do I Debug a Python file in VS Code?

If you're only interested in debugging a Python script, the simplest way is to select the down-arrow next to the run button on the editor and select Debug Python File in Terminal.

What is a breakpoint in VS Code?

Breakpoints are one of the most important debugging techniques in your developer's toolbox. You set breakpoints wherever you want to pause debugger execution. For example, you may want to see the state of code variables or look at the call stack at a certain breakpoint.


3 Answers

Change to:

"program": "${file}"
like image 99
jfriedman Avatar answered Oct 21 '22 17:10

jfriedman


For reference this is the full launch.json

{
  "launch": {
    "version": "0.2.0",
    "configurations": [
      {
        "name": "Node.js - Debug Current File",
        "type": "node",
        "request": "launch",
        "program": "${file}"
      }
    ]
  }
}
like image 36
Ricardo Mayerhofer Avatar answered Oct 21 '22 18:10

Ricardo Mayerhofer


For a single file, you can skip the launch.json file entirely. Just click the green arrow in the debugger panel and choose Node as your environment.

From here.

like image 1
samlandfried Avatar answered Oct 21 '22 18:10

samlandfried