Can't start debugging node - node.js

This is my first time use debugger on Visual Studio Code. I am working on a asp.net-react webapi. I was easily able to use debugger for .net Core. Here is my launch.json:
{
"version": "0.2.0",
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/API/bin/Debug/net6.0/API.dll",
"args": [],
"cwd": "${workspaceFolder}/API",
"stopAtEntry": false,
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
"serverReadyAction": {
"action": "openExternally",
"pattern": "\bNow listening on:\s+(https?://\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
Then I wanted to use debugger for node. I watched some videos from youtube but none of them worked. I also followed this link https://code.visualstudio.com/docs/editor/debugging, followed his steps but I nothing worked. I tried on launch, on attach, but the debugger doesn't even start. I added a onlaunch configuration:
{
"name": "Launch Program",
"program": "${workspaceFolder}/app.js",
"request": "launch",
"skipFiles": [ "<node_internals>/**" ],
"type": "node"
},
and it threw me this error on debug console:
C:\Program Files\nodejs\node.exe .\app.js
Process exited with code 1 Uncaught Error
Error: Cannot find module
'c:\Users\Lenovo\Desktop\Student\app.js' at
Module._resolveFilename (internal/modules/cjs/loader:933:15) at
Module._load (internal/modules/cjs/loader:778:27) at
executeUserEntryPoint (internal/modules/run_main:77:12) at
(internal/main/run_main_module:17:47)
No debugger available, can not send 'variables'
When I use onattach configuration:
{
"name": "Attach",
"port": 3000,
"request": "attach",
"skipFiles": [ "<node_internals>/**" ],
"type": "node"
},
it doesn't even throw any error, nor start debugging.
I googled and tried so many ways and nothing worked. Can anybody explain what should I do.
Thank you.

Related

Debugging multiple Node js Function apps in VS Code

I am currently developing a couple of node js function apps in the same project that I would like to be able to debug together. After some effort I was able to run them without a debugger with compounds. And it kinda works since all functions are running although im only able to see output from one at a time.
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Func1",
"type": "node",
"request": "launch",
"port": 9000,
"preLaunchTask": "npm: start - func1"
},
{
"name": "Func2",
"type": "node",
"request": "launch",
"port": 9001,
"preLaunchTask": "npm: start - func2"
}
],
"compounds": [
{
"name": "Compound",
"configurations": [
"Func1",
"Func2"
]
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "start",
"path": "func1/",
"problemMatcher": [],
"label": "npm: start - func1",
"detail": "func start"
},
{
"type": "npm",
"script": "start",
"path": "func2/",
"problemMatcher": [],
"label": "npm: start - func2",
"detail": "func start"
}
]
}
I have also tried different configurations in tasks.json and launch.json using func start with moderate success and I have been able to attach a single function app to debugger while starting the rest.
"type": "func",
"label": "func: start - func1"
"command": "host start",
"problemMatcher": "$func-node-watch",
"isBackground": true,
"dependsOn": "npm: install - func1",
"options": {
"cwd": "${workspaceFolder}/func1"
}
But when I attempt to add a second one with another label it is only able to recognize one of them and the launch of the other app receives error "Could not find the task 'func: start - func2'".
My questions are if it is possible to have multiple Function App attached to debugger and in that case how? Would it also be possible to get the logs in the same output terminal or multiple terminals for each Function App. I know that both can be achived in Visual Studio with c# but is it possible in VS Code with Node.
I am currently developing a couple of node js function apps in the
same project
1, If your function app here refers to Trigger, then the default settings of VS Code should be able to be achieved (unprocessed functions will be in a waiting state, but every place marked with a breakpoint will be executed, but you cannot be in different at the same time Breakpoints).
2, If your function app here refers to a standalone app, then the key is the port. Below settings can change the default port of function app on local:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "python"
},
"Host": {
"LocalHttpPort": 5861,
"CORS": "*",
"CORSCredentials": false
}
}
After the above settings, you can start two VS Code programs Debug different function app at the same time without causing conflicts.

VScode node debugger throws Uncaught Error: write EPIPE

Whenever I try to run the debugger, I keep getting this error:
This is the debug configurations:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/server/bin/www",
"envFile": "${workspaceRoot}/server/.env"
}
]
}
---- Update ----
I am using VScode version 1.49.0
As #S.Clarke876 mentioned, it appears to be a bug in VS Code, that should be fixed in the next release. See: https://github.com/microsoft/vscode/issues/106484#issuecomment-691202789
You can work around the issue by adding "outputCapture": "std" to your launch.json file.
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/server/bin/www",
"envFile": "${workspaceRoot}/server/.env",
"outputCapture": "std"
}
]
}
Worked for me! It only appeared to happen when the npm package debug attempted to write out to the console. I.e. console.log was fine.
Thanks again, #S.Clarke876! Only adding this answer as I nearly missed your comment.

Visual studio Code Debugger Not Working Fine For My Nodejs App

I am trying to use the vs code debugger for my node.js application and all the commands and code but when i apply the break point it goes back to the main files and function of the nodejs and npm files instead of going back step by step to the service and then storage, brake points not working properly for my apis any body can help
{
// 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": "attach",
"name": "Attach by Process ID",
"processId": "${command:PickProcess}"
},
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/app/app.js"
},
{
"type": "node",
"request": "launch",
"name": "Launch via NPM",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script",
"debug"
],
"port": 9229
},
{
"type": "node",
"request": "launch",
"name": "My Debugger",
"program": "${workspaceFolder}\\index.js"
}
]
}
Have you tried attaching to a node process?
Step 1: Run the code.
Step 2: Press CMD + SHIFT + P then search for "Attach to Node Process" and it will list the running processes.
Step 3: Choose the process you want to attach to.
This enables debugging.

Debug jasmine tests written in typescript node in vs code

I have my unit tests written in jasmine and those are in typescript
// about.service.spec.ts
// say 4 to 5 test cases
// spec/support/jasmine.json
{
"spec_dir": "src/tests/",
"spec_files": ["**/*.spec.ts"],
"helpers": ["jasmine-helpers/**/*.ts"],
...
}
// launch.json - vscode file
{
"version": "0.2.0",
"configurations": [{
"type": "node",
"request": "launch",
"name": "Jasmine tests",
"preLaunchTask": "debuggertests",
}]
}
// tasks.json - vscode
{
"version": "2.0.0",
"tasks": [{
"label": "debuggertests",
"type": "npm",
"script": "test:unit",
"problemMatcher": []
}]
}
// package.json
// have to use jasmine-ts which is flavor over ts-node
"test:unit": "jasmine-ts JASMINE_CONFIG_PATH=spec/support/jasmine.json"
I have used this configuration to debug .spec.ts files in vscode but it did not fire debugger instead it run all tests and debugging started.
I have put a breakpoint in one of the test case of about.service.spec.ts but no breakpoint fired. Could anyone help me on setting up vscode debugging for jasmine tests?
In new jasmine-ts version, you have to include the jasmine.json to the args as this:
{
"type": "node",
"request": "launch",
"name": "Jasmine Current File",
"program": "${workspaceFolder}/node_modules/jasmine-ts/lib/index",
"args": ["--config=jasmine.json", "${file}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
To avoid this issue:
No specs found
Finished in 0.003 seconds
Incomplete: No specs found
Randomized with seed 60766 (jasmine --random=true --seed=60766)
Below configuration will debug current test file - please open the required test file in VS Code and start debugging with this configuration:
{
"type": "node",
"request": "launch",
"name": "Jasmine Current File",
"program": "${workspaceFolder}/node_modules/jasmine-ts/lib/index",
"args": ["${file}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}

Debugging an Electron app : Breakpoints not working ("Breakpoint ignored because generated code not found")

I've got a basic HelloWorld app based on the electron-hello-world project running on VSCode and able to launch a debug session and the app starts up fine.
I've got a breakpoint set on main.js but it appears to be greyed out with a message:
Breakpoint ignored because generated code not found (source map problem?)
Here is my launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Main Process",
"type": "node",
"request": "launch",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
"program": "${workspaceRoot}/main.js",
"port": 9222,
"sourceMaps": true,
"diagnosticLogging": true,
"outFiles": [
"${workspaceRoot}"
]
}
]
}
Any help would be much appreciated.
If your generated code is present in "${workspace Root}" then outFiles should be "outFiles": [ "${workspaceRoot}/*.js" ].
For more info link

Resources