So I have created a new folder called "Node Projects" and I added it to "Workspace" on VS Code. I then created two sample files called "test.js" and "test2.js". In these files I simply have a single log command to determine which is running.
When I run the test.js, I get the error message "Attribute 'program' does not exist (C:\Users\MyName\Documents\NodeProjects/Node Projects/test.js' so I click on "Open launch.json" button and see this:
{
// 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": "Launch Program",
"program": "${workspaceFolder}/Node Projects\\test.js"
}
]
}
Based on my search here on stackoverflow, I believe the problem is with the "program" line, so I go ahead and change it to
"program": "${workspaceFolder}\\test.js"
So now, when I do a F5, the debugger runs the test.js which is great. But because the test.js value is hard-coded, even when I open test2.js on the editor and do an F5, it runs test.js again! So I tried removing the file name (i.e. test.js) from launch.json. But now when I try to run a file, I get the error that "Cannot launch program". Oh and deleting the launch.json didnt help either (it just recreated the initial launch.json file and I was back to square one).
Try this,
"program": "${file}"
${file} is a predefined variable in VS Code for the current opened file.
See https://code.visualstudio.com/docs/editor/variables-reference
Add another key "cwd", and it works for me.
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/xxx/xxx.js",
"cwd": "${workspaceFolder}"
}
]
Related
The script that is meant must be executed with the command
node --no-experimental-fetch server.js
otherwise a error occurs.
To use the VS Code-debugger and not to start the script always with the shell,
I want to integrate the command in the launch.json.
I generated and edited the following launch.json
{
// 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": "Start server",
"skipFiles": [
"<node_internals>/**"
],
"args": [
"--no-experimental-fetch"
],
"program": "${workspaceFolder}/server.js"
}
]
}
but the error occurs.
Has someone an idea what should be edit in the launch.json that fixes the issue?
Thanks for reading and answering this question, I appreciate it.
Instead of "args" you should use "runtimeArgs" with arguments for node(.exe).
You might as well use environment variable definition for "NODE_OPTIONS":
...
env: {
"NODE_OPTIONS": "--no-experimental-fetch"
},
...
For some reason the VS Code debugger doesn't stop at breakpoints in recently created node.js projects. In some of my older projects it works like a charm. I use the same launch files but it wont work. The debugger starts and runs through but it doesn't stop on any breakpoints.
Someone had the same issue? I recognized this after the latest update but I can't find some forum entries describing my problem.
I'm running the VS Code Version 1.50.1 and Node.js Version 14.13.0.
This is the launch file of an older project which works:
{
"type": "node",
"request": "launch",
"name": "Launch LN Star",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}\\NL_Star\\main.js",
"console": "integratedTerminal"
}
This is the launch file of a new project which runs but doesn't stop on any breakpoint
{
// 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": "Launch",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}\\index.js",
"console": "integratedTerminal"
}
]
}
I'm glad to add some more information if needed and when I know how or where to look for it. But for now I don't really know where to start to fix this.
I guess I just missed something but I cant really find it!
Thanks a lot in advance for your help!
I am just starting to learn using the Nodejs debugger. It has been really helpful already but I spent a a lot of time changing a js file I was debugging but I did not know that the changes I made were not being implement right away.
Is this an expected behavior? Can I set the debugger up so that it restarts on each save and notices the new changes?
Edit 1 :
Here is my debuf config:
{
// 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": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\app.js"
}
]
}
Normally, after every save, you have to restart your node program to see the changes. I'm sure there are other programs but one I use exclusively (I never use node alone) is nodemon. It's globally installed npm package program. Here is sample config for vs code debugger:
{
"type": "node",
"request": "launch",
"name": "Launch app - nodemon",
"runtimeExecutable": "nodemon",
"runtimeArgs": [
"--inspect=9250"
],
"program": "${workspaceRoot}/api/app",
"cwd": "${workspaceRoot}/api",
"autoAttachChildProcesses": true,
"restart": true
},
nodemon will monitor all the files and restart node every time it detects changes.
Add runtimeArgs to change debugger port.
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.
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"
}
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}"
}
]
}
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 help with any confusion, your debug options depend on how your workspace is setup:
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.
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.
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.
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
I created simple node.js application (source code from here https://azure.microsoft.com/en-us/blog/visual-studio-code-and-azure-app-service-a-perfect-fit/)
var http = require('http');
http.createServer(function (req, res) {
console.log('Got request for ' + req.url);
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<h1>Hello Code and Azure Web Apps!</h1>');
}).listen(process.env.PORT);
And clicked VSCode generated launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/app.js",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
"preLaunchTask": null,
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
],
"env": {
"NODE_ENV": "development"
},
"externalConsole": false,
"sourceMaps": false,
"outDir": null
},
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858,
"address": "localhost",
"restart": false,
"sourceMaps": false,
"outDir": null,
"localRoot": "${workspaceRoot}",
"remoteRoot": null
}
]
}
And still when launched I see:
Attribute 'program' does not exist.
Can anybody help what's wrong?
I believe that you need ${workspaceRoot}/server.js, not ${workspaceRoot}/app.js for program. The code you're using doesn't have an app.js, that's what that (poorly worded) error is telling you.
I also encountered this issue because of where VS Code put the .vscode directory containing the launch.json file. It put it up one directory so I had to add the directory to the path as defined in the launch.json file:
"program": "${workspaceRoot}/myDir/app.js",
I hope this helps.
Another issue I ran into is a path was configured Using\\Backslashes\\Like\\So and worked fine on Windows, but on Mac it gave the above error.
(Solution: changed to /)
The error is saying that the path to your code was wrong.
VSCode defines the parent directory of its configuration file ".vscode/launch.json" as "${workspaceRoot}" or "${workspaceFolder}".
So, for example, if you want to run file "myproject/subfolder/main.js", you should configure your "myproject/.vscode/launch.json" as follows:
"program": "${workspaceRoot}/subfolder/main.js"
Note that configuring
"program": "${workspaceRoot}/myproject/subfolder/main.js"
is a mistake and will cause error "Attribute 'program' does not exist".
I wasted a few hours today trying to figure this problem out. What worked for me was deleting the existing launch.json and running the application, which prompts you to select an enviroment, which in my case was Node. This created a new launch.json in which I updated the program path.
The error should ideally read 'file specified in program attribute does not exist' because that is what is happening. As of VSCode 1.30.2, it does show you the path along with the error.
In my case I had
"program": "${workspaceFolder}\\${file}" so the path was something like c:\dir\c:\dir\file.js
I corrected this by removing ${workspaceFolder} since I wanted to be able to debug individual files.
I had the same issue. In my case my launch.json had following line
"program": "${workspaceFolder}\\index.js"
My active code that I tried to debug was in app_v2.js , so I updated it to following, and then debug worked.
"program": "${workspaceFolder}\\app_v2.js"
Firstly, read the official document this answers all question you would have about setting the right attributes for different scenarios using launch.json.
Now, to specifically answer this question, the ${workspaceFolder} is basically containing directory of the .vscode directory, which is your project root directory. So, when setting specific files as your debugging program, remember to map the path from the project root directory, or in other words the relative path of the file that is to be set as the debugging program. This can be easily obtained from the IDE (VS Code) by simply right-clicking the file and selecting the Copy Relative Path option. Then proceed to paste this next to the ${workspaceFolder} in the program attribute in your launch.json file, like below, will fix the problem.
"program": "${workspaceFolder}/<relative_path>"
Replace relative path with your copied relative path as mentioned before
Note that I am on a Mac platform. Please use platform appropriate path separators
Alternatively, not specifically using a launch configuration makes sense if it's a not-for-production or a simple app that does not warrant a launch config file. However, if not, it is super useful when debugging in a Multi-target environment (server, client). In my opinion, using a compound launch configuration setup makes things a lot easier. Read this section of the official docs to learn how to set it up keeping in mind the relative paths of your server and client files.
I had the same question and took me couple of hours to figure it out. What I basically did was that I deleted the folder after ${workspaceFolder}
The format was ${workspaceFolder}/xxxx\\folder\\subfolder\\subfolder
so by deleting what's after the "workspaceFolder" and starting my path from the double backward slash, it did fix it for me.
I had the same error, because I was passing the arguments inside "program" attribute like this:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Build -B -p",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\app\\build -B -p D:\\apps\\12"
}
]
}
What solved for me was to pass the arguments inside "args" attribute, like this:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Build -B -p",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\app\\build",
"args":["-B", "-pD:\\apps\\12"]
}
]
}
The O.S. was Windows 7.
It simply means that when you debug the file the app.js file simply doesn't exist and when you perform debugging it shows the error.
Here it my way to fix the problem :
simply replace the value of program
"${workspaceRoot}/app.js"
by
"${workspaceFolder}/${fileBasenameNoExtension}.js"
I hope this will solve all of your problems.
For the ones who are using Visual Studio 2019, today I was trying Node.js in the "master" VS2019. I moved server.ts to the src folder so that my js output would be in lib folder.
After that I tarted getting that message. Here are the changes I made in my project file to have it working.
<StartupFile>lib\server.js</StartupFile>
<WorkingDirectory>lib</WorkingDirectory>
<OutputPath>lib</OutputPath>
I hope this is not out of topic, it could help VS IDE users.