Search code examples
node.jsserverless-frameworkvscode-debugger

Why won't my Serverless Framework VSCode debug configuration execute?


I am using Serverless Framework to create a node API backend for my web app. VSCode is my IDE, running on Windows 11.

The VSCODE Run and Debug widget fails immediately with an error that I cannot comprehend or fix.

Here is my launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "run SLS",
      "program": "${workspaceRoot}/api/fq-transcribe/node_modules/.bin/sls",
      "args": ["invoke", "local", "-f", "fqTranscribe", "-p", "./test-events/event.json"]
    }
  ]
}

Here is the error I get:

Uncaught SyntaxError C:\Users\david\build\frequency\api\fq-transcribe\node_modules\.bin\sls:2
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
          ^^^^^^^

SyntaxError: missing ) after argument list
    at compileFunction (vm:360:18)
    at wrapSafe (internal/modules/cjs/loader:1084:15)
    at Module._compile (internal/modules/cjs/loader:1119:27)
    at Module._extensions..js (internal/modules/cjs/loader:1209:10)
    at Module.load (internal/modules/cjs/loader:1033:32)
    at Module._load (internal/modules/cjs/loader:868:12)
    at executeUserEntryPoint (internal/modules/run_main:81:12)
    at <anonymous> (internal/main/run_main_module:22:47)
vm:360
Process exited with code 1

Here is the SLS file:

#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")

case `uname` in
    *CYGWIN*|*MINGW*|*MSYS*) basedir=`cygpath -w "$basedir"`;;
esac

if [ -x "$basedir/node" ]; then
  exec "$basedir/node"  "$basedir/../serverless/bin/serverless.js" "$@"
else 
  exec node  "$basedir/../serverless/bin/serverless.js" "$@"
fi


Solution

  • With some help from a colleague, I got this working. I can now do breakpoint debugging on Serverless Offline and with Serverless Invoke Local on Windows. We also know how to do Mac and Linux, I'll share that as well.

    Here is the launch.json file. The first config is for Serverless Offline, the second config is for Invoke Local. You may have to update the cwd to reach the directory with your serverless.yml file.

    {
      "version": "0.2.0",
      "configurations": [
          {
              "type": "node",
              "request": "launch",
              "name": "Debug Serverless Offline",
              "cwd": "${workspaceFolder}",
              "skipFiles": [
                  "<node_internals>/**"
              ],
              "args": [
                  "run",
                  "debug"
              ],
              "sourceMaps": true,
              "runtimeExecutable": "npm"
          },
          {
            "type": "node",
            "request": "launch",
            "name": "Debug Serverless Invoker",
            "cwd": "${workspaceFolder}",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "args": [
                "run",
                "invoker"
            ],
            "sourceMaps": true,
            "runtimeExecutable": "npm"
        }
      ]
    }
    

    Add this to your package.json:

     "scripts": {
        "debug": "node --inspect node_modules/serverless/bin/serverless offline -s dev --reloadHandler",
        "invoker": "node --inspect node_modules/serverless/bin/serverless invoke local -f YourFunctionNameHere"
    
      },
    
    

    For Mac/Linux, it's exactly the same except one thing: In your package.json: runtimeExecutable must be the full path to your npm. Use which npm to get the value.