dherault / serverless-offline

Emulate AWS λ and API Gateway locally when developing your Serverless project
MIT License
5.19k stars 795 forks source link

Debug in VS code #1700

Closed dreamfalcon closed 1 year ago

dreamfalcon commented 1 year ago

Hi,

I'm trying to follow the documentation in how to set up the debug on Vs code. But the breakpoints and hot reloading are not working.

Until now I have this:

package.json

  "scripts": {
    "test": "jest ./src --runInBand",
    "build": "npx tsc",
    "start": "node bin/index.js",
    "lint": "eslint . --ext .ts && prettier --write \"**/*.ts\"",
    "debug": "SET SLS_DEBUG=* && node --inspect node_modules\\serverless\\bin\\serverless offline"
  },

launch.json:

{
      "cwd": "${workspaceFolder}",
      "name": "Debug Serverless Offline",
      "request": "launch",
      "runtimeArgs": ["run", "debug"],
      "runtimeExecutable": "npm",
      "sourceMaps": true,
      "type": "node",
      "env": {
        "ENVIRONMENT": "dev"
      }
    }

Any idea whats wrong?

dreamfalcon commented 1 year ago

Managed to find a way.

1 - Run tsc with npx tsc --watch

2 - Enable "sourceMap": true on tsconfig.

3 - Add reloadHandler to debug command.

  "scripts": {
    "debug": "SET SLS_DEBUG=*  && node --inspect node_modules\\serverless\\bin\\serverless offline --reloadHandler"
  },

4 - Put the environment variables on the launch.json.

{
      "cwd": "${workspaceFolder}",
      "name": "Debug Serverless Offline",
      "request": "launch",
      "runtimeArgs": ["run", "debug"],
      "runtimeExecutable": "npm",
      "sourceMaps": true,
      "type": "node",
      "env": {
        "ENVIRONMENT": "dev",
      }
    }

Now I'm able to use breakpoints, watch the variables and hot reload works.