14

I have tried to debug serverless application developed using serverless framework in VS code. I have followed this article.

But when I trying to debug the code I'm getting an error from VS code as below.

Cannot launch program 'g:\Projects\Serverless1\node_modules.bin\sls'; setting the 'outDir or outFiles' attribute might help.

sls command file already exists in the folder and following are the launch.json file settings

"version": "0.2.0",
"configurations": [

    {
        "type": "node",
        "request": "launch",
        "protocol": "inspector",
        "name": "run hello function",
        "program": "${workspaceRoot}\\node_modules\\.bin\\sls",
        "args": [
            "invoke",
            "local",
            "-f",
            "hello",
            "--data",
            "{}"
        ]

    }
]

Please help me to fix this issue.

Wella
  • 1,386
  • 3
  • 15
  • 24
  • Facing exact same issue. It is working from console but not through the debug configuration. Please let me know if you find a solution. – Mehaboob Khan Aug 03 '17 at 08:16

4 Answers4

23

I attempted to follow the same article, and experienced the same error. Adding outFiles didn't help, although it did change my error message to:

Cannot launch program 'd:\<path>\node_modules\.bin\sls' because corresponding JavaScript cannot be found.

I can't explain why VSCode has a problem with the executable in node_modules/.bin, but if I point at node_modules/serverless/bin instead, things work as expected:

"program": "${workspaceFolder}\\node_modules\\serverless\\bin\\serverless",

Here is my full working configuration, where my test event JSON exists in sample-event.json in the project root:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Debug Lambda",
            "program": "${workspaceFolder}/node_modules/serverless/bin/serverless",
            "args": [
                "invoke",
                "local",
                "-f",
                "<function-name>",
                "--data",
                "{}" // You can use this argument to pass data to the function to help with the debug
            ]
        }
    ]
}

Using Serverless ^1.26.1, Node 8.9.4 LTS, VSCode 1.20.1

João Miguel
  • 163
  • 1
  • 4
Mike Patrick
  • 10,699
  • 1
  • 32
  • 54
  • 1
    Worked like a charm. – João Miguel Mar 06 '18 at 22:27
  • THANK YOU! Was pulling my hair out over this. – MungeWrath Mar 09 '18 at 23:15
  • In my situation, even though I had indicated ${workspaceRoot}/node_nodules/serverless/bin/serverless and that path resolved correctly to a --save-dev install of the actual serverless module, VS Code would not run serverless, resulting in Attribute 'program' does not exist. However, changing program to /Users/username/.nvm/versions/node/v8.10.0/bin/serverless did work. – jarmod Mar 21 '19 at 13:43
2

To get debugging to work with TypeScript I needed to add outFiles set to the folder where my compiled code goes.

"outFiles": [
    "${workspaceRoot}/dist/**/*.js"
]

I haven't tried to debug straight JS but I would assume it's something like this.

"outFiles": [
    "${workspaceRoot}/**/*.js"
]
Rich Buggy
  • 289
  • 1
  • 5
  • i have tried adding outFiles but still getting same error. its not related with this and VS code always give this commen error when any other error occured. etting the 'outDir or outFiles' attribute might help. – Wella Aug 05 '17 at 16:24
2

None of the solutions worked for me so here is my modification as a resource. Also, multiple coworkers were able to attack just by flipping auto-attach to on and using the invoke local keywords.

Below is a snippet featuring the launch.json that eventually worked for me. /w comments for clarity where my function is named Processor.

--function or -f The name of the function in your service that you want to invoke locally.

--path or -p The path to a json file holding input data to be passed to the invoked function as the event. This path is relative to the root directory of the service.

--stage or -s The stage in your service you want to invoke your function in.

  • "serverless": "^1.30.3"
  • "serverless-plugin-typescript": "^1.1.5",
  • node: 8.10.0
  • npm: 5.6.0

    {
      "version": "0.2.0",
      "configurations": [
          {
              "type": "node",
              "request": "launch",
              "name": "Debug Lambda",
              "program": "${workspaceFolder}/node_modules/.bin/sls",
              "args": [
                  "invoke",
                  "local",
                  "-f",
                  "Processor",
                  "-p",
                  "./events/S3toLambda.json",
                  "-s",
                  "local"
              ],
              "autoAttachChildProcesses": true
          }
      ]
    }
    
DisplayName
  • 196
  • 3
  • 12
2

Update for 2020:

Do what the other guides state and setup your project with a launch.json file.

The problem I had was that the supposed file "program": "${workspaceRoot}/node_modules/.bin/sls" threw an error.

I changed it to "${workspaceRoot}/node_modules/serverless/bin/serverless" and it worked. Here's the full file:

.vscode/launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Serverless debug",
            "program": "${workspaceRoot}/node_modules/serverless/bin/serverless",
            "args": [
                "invoke",
                "local",
                "-f",
                "hello",
                "--data",
                "{}"
            ]
        }
    ]
}

Be aware that the argument hello is the name of the function I want to debug. I think the intended use case must be that you change that file name for whatever function you want to invoke. Maybe someone could create a VSCode plugin to do this?

MSOACC
  • 3,074
  • 2
  • 29
  • 50