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.
Related
I am having trouble finding a solution for running the VS Code integrated Debugger with Browser-Sync. Instead of launching chrome with the VS Code Debugger, my current launch.json looks like this:
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Chrome",
"port": 9222,
"request": "attach",
"type": "pwa-chrome",
"url": "http://localhost:3002",
"webRoot": "${workspaceFolder}/dist"
}
]
}
This solution let's me launch browser-sync via my taskrunner ("gulp") in my terminal, however, it does not let me set breakpoints within VS Code:
As soon as I pause the debugger, I will end up somewhere in the depths of the browsersync.js files (see Screenshot here)
Is there a better way of debugging within VS Code whilst using gulp and retaining automatic browser reload?
Ok, after making the comment I decided to give another try and it seems that everything is working finally!
My case is a little bit special because I have a browser-sync setup mapping to several other folders + typescript compilation + rollup + multi-root workspace + an in-house made framework + other things to handle. But in the end, a simple solution was the one that worked for me:
{
"version": "0.2.0",
"configurations": [
{
"type": "pwa-chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:3000",
"pathMapping": {
"/": "${workspaceFolder}/dist",
}
}
]
}
The thing here is the pathMapping property that helps the debugger find the source map files. In my case, these files are mapped (with browser-sync) to the '/' path but they are inside the '/dist' folder inside my project root.
Keep in mind that if you have typescript source files like I do, you absolutelly need to generate source map files setting "sourceMap": true inside your tsconfig.json file.
EDIT Another thing that I forgot to mention is that the browser-sync server must be running in another terminal before you start the debug.
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!
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}"
}
]
I am running Jest test cases into a reactjs project. I am trying to debug my jest test cases in VS code. The tests run ok on command line. But when I launch debugger in VS code I see error.
Launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest Tests",
"program": "${workspaceRoot}/xxx/xxx/node_modules/jest/bin/jest",
"args": [
"-i"
],
"internalConsoleOptions": "openOnSessionStart",
"outFiles": [
"${workspaceRoot}/xxx/xxx/**/*"
]
}
]
}
Error
Debugger listening on ws://127.0.0.1:31182/2bf113f1-002f-49ed-ad91-5510affd172a
Debugger attached.
Error: Could not find a config file based on provided values:
path: "/Users/xxx/xxx/xxx-ui"
cwd: "/Users/xxx/xxx/xxx-ui"
Configh paths must be specified by either a direct path to a config
file, or a path to a directory. If directory is given, Jest will try to
traverse directory tree up, until it finds either "jest.config.js" or
"package.json".
Just specify -c option in your launch config:
{
"name": "Debug Jest Tests",
"type": "node",
"request": "launch",
"runtimeArgs": [
"--inspect-brk",
"{$PathToNPMRoot}/node_modules/jest/bin/jest.js",
"--runInBand",
"-c", "{$PathToConfig}/jest.config.js",
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"port": 9229
},
You will have to replace {$PathToNPMRoot} and {$PathToConfig}
Source
I'll tell you what did the trick for me.
As documentation says, in case that your code is not in the root of your project folder you should create a new config file to specify where jest is located in z .vscode folder, for example:
.vscode/settings.json
{
"jest.pathToJest": "functions/node_modules/.bin/jest"
}
(You should replace "functions" to your own folder name, in case you have your code on your project's root you won't need to do the previous step)
After doing that I was still getting the same error as you even though I configured correctly the path to jest and my jest configuration was specified correctly on my package.json, then I moved my jest configuration to jest.config.js file on the root of my project and that was all, jest was now up and running after I used the command Jest: Start Runner.
//jest.config.js on root dir
module.exports = { verbose: true };
I hope you can get it working
cheers!
I never created a Jest config file. Instead, I just set the working directory in the VSCode launch.json file:
How to configure vs code working directory in the 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": [
// https://jestjs.io/docs/troubleshooting
{
"name": "Debug Jest Tests for Modules",
// https://stackoverflow.com/questions/47540627/
// how-to-configure-vs-code-working-directory-in-the-launch-json
"cwd": "${workspaceRoot}/pathToMyJestModuleTestDir",
"type": "node",
"request": "launch",
"runtimeArgs": [
"--inspect-brk",
// https://stackoverflow.com/questions/60372790/
// node-v13-jest-es6-native-support-for-modules-without-babel-or-esm
"--experimental-vm-modules",
"${workspaceRoot}/pathToMyJestModuleTestDir/node_modules/jest/bin/jest.js",
"--runInBand"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"port": 9229
}
]
}
Values are for illustrative purposes only. They do not reflect what I use on my development machines.
The main thing to note here is the following line:
"cwd": "${workspaceRoot}/pathToMyJestModuleTestDir",
That line is explained in the SO Thread mentioned at the top of this answer.
I am new to Jest, as so I had this problem several times! and came back here several times, no solution! nothing worked. No matter how stupid it sounds, the problem was lacking the package.json, you cannot run without it. Maybe it is somewhere on the documentation, I am not very patient to keep reading documentation! Thus, test the obvious: if you created your package.json!!!
Just type in the following and you will be good to go.
npm init
I tried to use the simple node.js example visualstudio code provides, but unfortunately when node is being started from visual studio code (on OS X) the node gets started with some arbitrary debug-brk, but debugger attachment fails.
When I run the node app manually with --debug-brk = 5858 and then use the attach I can debug my app. Anyone faced the same problem?
P.S I got mono installed from the mono project page .pkg
Here is my launch.js:
{
"version": "0.1.0",
// List of configurations. Add new configurations or edit existing ones.
// ONLY "node" and "mono" are supported, change "type" to switch.
"configurations": [
{
// Name of configuration; appears in the launch configuration drop down menu.
"name": "Launch app",
// Type of configuration. Possible values: "node", "mono".
"type": "node",
// Workspace relative or absolute path to the program.
"program": "./bin/www",
// Automatically stop program after launch.
"stopOnEntry": true,
// Command line arguments passed to the program.
"args": [],
// Workspace relative or absolute path to the working directory of the program being debugged. Default is the current workspace.
"cwd": ".",
// Workspace relative or absolute path to the runtime executable to be used. Default is the runtime executable on the PATH.
"runtimeExecutable": null,
// Environment variables passed to the program.
"env": { }
},
{
"name": "Attach",
"type": "node",
// TCP/IP address. Default is "localhost".
"address": "localhost",
// Port to attach to.
"port": 5858
}
]
}
Just ran into the same issue... Code couldn't find Node.
Change this line to point to your executable, for example:
"runtimeExecutable": "C:/Program Files/nodejs/node.exe",
Enjoy!
Might be the cluster thing. Try to turn off cluster in ./bin/www, and you will be able to debug it.