'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.
Solution 1:[1]
There are two ways to achieve this:
Just add
launch.jsonand give yourfile_name. and start debugging.For example, If your
file_nameisindex.js. create a folder called.vscodeand inside this folder createlaunch.json, structure looks like this:main_folder |___ index.js |___ .vscode |___ launch.jsonand 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.jsonand give your file an entry point. when you press F5,vscodewill consider this file as starting point.main_folder |___ index.js |___ package.jsonyou can create
package.jsonmanually or can create it usingnpm init, This will ask you a bunch of questions, and then write apackage.jsonfor you.{ "name": "application_name", "version": "0.0.0", "description": "for single page debugging", "main": "index.js", "author": "", "license": "ISC" }
Solution 2:[2]
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}"
}
]
}
Solution 3:[3]
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.
Solution 4:[4]
To help with any confusion, your debug options depend on how your workspace is setup:
If you have not created a
launch.jsonfile, you will see the following options in the debug panel. ClickingRun and Debugwill debug the currently active file.If you have a
package.jsonfile, you will still see the same view shown above; however, VSCode will first try to debug the file name you have specified in themainattribute of yourpackage.json. If it does not find that file, it will then debug the currently active file. So, for instance, if mypackage.jsonshowsindex.jsas mymainfile, then VSCode will always run that file in the debugger if it can find it instead of your currently active file.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>/**" ] } ] }
For more details, check-out Debugging in Visual Studio Code.
Solution 5:[5]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | kgangadhar |
| Solution 3 | samlandfried |
| Solution 4 | kgangadhar |
| Solution 5 | Rob McFeely |


