Running netlify functions in a debugger

I’m trying to run netlify functions in the vscode debugger.

I have a very simple hello world netlify function from this tutorial. I’m using netlify-lambda to run the function locally.

exports.handler = async event => {
  const subject = event.queryStringParameters.name || 'World';
  debugger;
  console.log(event);
  return {
    statusCode: 200,
    body: `Hello ${subject}!`
  };
};

I have read the debugging section of the netlify-lambda docs but it’s not clear to me that it’s possible for the debugger to connect to the process, it only mentions debug logging.

I tried:

npx --node-arg=--inspect-brk=$DEBUG_PORT --node-arg=--nolazy netlify-lambda serve ./lib/functions

node --nolazy --inspect-brk=$DEBUG_PORT ./node_modules/.bin/netlify-lambda serve ./lib/functions

In both these cases the server starts and the logs say the debugger has connected but when I load the function in the browser though it runs the code the debugger does not stop. I also tried adding a ‘debugger’ statement in the function, but that did nothing.

Is it possible to run the functions in the vscode debugger, if so how?

I’m not sure as I’m not familiar with how vscode debugger. Just to mention, the first command (with npx) should work as far as getting the netlify-lambda server running but I’m not sure about the DEBUG_PORT. The server does run on port 9000, so perhaps you can set that in your debugger? That’s just a guess since I don’t how that debugger works, to be honest. Let me know if that helps.

@Dennis Thanks for the reply. Typically to configure the vscode debugger you have some npm scripts in package.json:

// package.json
"scripts": {
  "start": "node app.js",
  "debug:start": "node --nolazy --inspect-brk=$DEBUG_PORT app.js"
}

Then in your project root create the vscode config:

// .vscode/launch.json
{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "npm run debug:start",
      "runtimeExecutable": "npm",
      "runtimeArgs": [
        "run-script",
        "debug:start"
      ],
      "port": 9229,
      "outputCapture": "std"
    }
  ]
}

In vscode the launch config appears in the debug menu and when you select it, it runs the application and connects to the debug port and then you can control execution via breakpoints etc.

But I’m not sure how to configure it when running the app via netlify-lambda. Maybe netlify-lambda needs to pass these values to the node process?

there’s some info on this in the issues: https://github.com/netlify/netlify-lambda/issues/148 but unfortunately I don’t have much experience with attaching the debugger. Hopefully this helps you figure it out though and PR welcome if we need to expose something.