Can't debug serverless app in Visual Studio Code - node.js

I'm trying to find out how can I use Visual Studio Code debugger to debug serverless lambda function. For testing purposes I have very simple test lambda function:
module.exports.handler = async (event) => {
debugger;
console.log(111, event);
};
Then, in launch.json I created such a configuration:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch via NPM",
"runtimeVersion": "8.4.0",
"runtimeExecutable": "node",
"program": "${workspaceFolder}/node_modules/.bin/sls",
"cwd": "${workspaceRoot}",
"args": [
"invoke",
"local",
"--function",
"test",
"--data",
"{foo:'bar'}"
],
"port": 9229
}
]
}
Now, I'm puting a breakpoint inside my lambda function and pressing F5 to start debugger. I'm expecting then to go in code, put some watches and walk my code step-by-step. But nothing happens. No errors. No console outputs, no code paused on breakpoints. Nothing. All I get is message in debug console: /home/set/.nvm/versions/node/v8.4.0/bin/node node_modules/.bin/sls invoke local --function test --data {foo:'bar'}
If I go then in terminal and run that line I got exactly what is expected
set#set-home ~/www/blahblah/ens $ /home/set/.nvm/versions/node/v8.4.0/bin/node node_modules/.bin/sls invoke local --function test --data {foo:'bar'}
Serverless: INVOKING INVOKE
111 '{foo:bar}'
What am I doing wrong? How can I debug serverless application in Visual Studio Code?

I suspect you just need to get rid of the "port": 9229 bit.
I've never specified a port for debugging Serverless functions locally, but adding it to any of my working configurations produces the symptoms you observe.
Incidentally, you might be able to take some of the other stuff out, as well. For reference, my Serverless debug configurations typically look like this for invoking locally:
{
"type": "node",
"request": "launch",
"name": "Debug sls local",
"program": "${workspaceFolder}/node_modules/serverless/bin/serverless",
"args": [
"invoke", "local", "--function", "processFirehose", "--path", "sample-event.json",
"--stage",
"nonprod",
]
},

Related

Debug AWS SAM in Visual Studio Code

I am trying to debug my \event endpoint using AWS SAM in VSCode and I'm having issues with two of the configurations I've tried. The Direct Invoke Configuration I've used successfully in a different project, but in my current project it returns this error even though template.yaml and package.json are both in the EventsApi project directory.
{
"type": "aws-sam",
"request": "direct-invoke",
"name": "Direct Invoke Config",
"invokeTarget": {
"target": "api",
"templatePath": "template.yaml",
"logicalId": "AddEventFunction"
},
"api": {
"path": "/event",
"httpMethod": "post",
"payload": {
}
}
}
which produces this error
[ERROR]: SamLaunchRequestError: Failed to run launch configuration
-> Error: Cannot find package.json for: C:/aws/EventsApi/template.yaml
And the second configuration, which I attach in the SAM CLI to my AddEventFunction lambda in debug mode, then the debugger catches exceptions but won't hit any endpoints with this configuration. Please note that stopOnEntry is unable to be added to the attach configuration for some reason.
{
"name": "Attach to SAM CLI Config",
"type": "node",
"request": "attach",
"address": "localhost",
"port": 5858,
"localRoot": "${workspaceRoot}/src/handlers",
"remoteRoot": "",
"sourceMaps": true,
},
I expect the first configuration to find package.json just fine, and the second configuration to hit a breakpoint.

VS Code debugger is inconsistent while hitting breakpoints

I want to set up my VS Code debugger to debug my Next.js app. My launch.json contains the below settings:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Launch Program",
"skipFiles": ["<node_internals>/**"],
"port": 9229,
"resolveSourceMapLocations": [
"${workspaceFolder}/**",
"!**/node_modules/**"
]
}
]
}
I also added the debug script to my packege.json:
"debug": "NODE_OPTIONS='--inspect' next dev"
First, I run npm run debug in my terminal, then I launch the debugger in the VS Code Run and Debug tab. The debugger is attached, but when I start adding breakpoints, some of them work properly (the dot indicating the breakpoint is red and the app stops when the breakpoint is hit), but most of them are ignored despite the red indicator. There're also some unbound breakpoints.
I spent a few days figuring this out with no effect. I really look forward to moving past console-logging for debugging purposes.
VS Code version: 1.57.0

VSCode Nodejs debugger does not save changes?

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.

VSCode - debugger not hitting break-point on postman request

I'm building an express.js application that I'm testing using Postman. I haven't needed debugging until now, but I've just tried to set it up and run into a bit of an issue.
I followed this tutorial, but it isn't working for me as the end result of the tutorial shows that it should.
I built myself a launch.json file with a configuration built to handle running using nodemon...
"configurations": [
{
"type": "node",
"request": "launch",
"name": "nodemon",
"runtimeExecutable": "nodemon",
"program": "${workspaceFolder}/app.js",
"restart": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}\\bin\\www"
}
]
I then started the debugger and got the following output in my terminal...
C:\Users\User\AppData\Roaming\npm\nodemon.cmd --inspect=35939 --debug-brk app.js
[nodemon] 1.14.11
[nodemon] to restart at any time, enter rs
[nodemon] watching: .
[nodemon] starting node --inspect=35939 --debug-brk app.js
Debugger listening on
ws://127.0.0.1:35939/fca1b410-e096-4189-a5b2-bf266755e89e
For help see https://nodejs.org/en/docs/inspector
Debugger attached.
(node:24832) [DEP0062] DeprecationWarning: node --inspect
--debug-brk is deprecated. Please use node --inspect-brk instead.
To test it out, I put a break-point on the very first line of my app.js file. The break-point was hit successfully.
I went into the router file and put a break-point inside the post method that I needed to debug...
//POST New Trip page//
router.post('/newtrip', function (req, res) {
return tripCtrl.create(req, res); <---- BP on this line
});
I then ran the Postman request that I have been using to test this method:
(POST) http://localhost:3000/newtrip
I was fully expecting my new break-point to be hit, but the break-point wasn't hit, and I got no output from my debugger console or terminal. All I have is Postman returning 'Could not get any response'.
I'm well and truly lost as to where to go. Can anyone spot what might be wrong here please?
This is my file structure for the project:
Thanks,
Mark
This is the minimum configuration of lanuch.json required to debug in vscode based on your start command. Try updating with this to debug.
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/bin/www"
}
]
}

Debug/Run in vscode: Program exists

I have an express.js app.
Whenever I try to debug/run it in vscode, it just runs through and exists. How do I keep this process alive just like it normally does? Or is something wrong with my config?
No errors whatsoever are thrown.
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "www",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script",
"www"
],
"address": "localhost",
"port": 5000,
"stopOnEntry": false,
"cwd": "${workspaceRoot}",
"env": {
...
}
}
]
My npm-script looks like this:
flow-node www/index.js
I want to debug/run this script from vscode since I have a lot of env-variables and, obviously, for debug-reasons.
You may just need to specify the program attribute of the launch.json config.
program - executable or file to run when launching the debugger
https://code.visualstudio.com/docs/editor/debugging#_launchjson-attributes

Resources