How to debug aws lambda functions written in node js

后端 未结 9 3272
孤城傲影
孤城傲影 2021-02-20 13:34

We have been developing AWS Lambda functions in Node JS for a few months. Can we debug, i.e. step through the Node JS code as we can with .Net C# code in Visual Studio?

9条回答
  •  半阙折子戏
    2021-02-20 14:00

    If you are using serverless and serverless offline plugin to test in local. You can refer below:

    If you are using windows, update the vscode launch.json and package.json as below :

    // launch.json
    {
    
        "version": "0.2.0",
    
       "configurations": [
    
           {
    
               "type": "node",
    
               "request": "launch",
    
               "name": "Debug Serverless",
    
               "cwd": "${workspaceFolder}",
    
               "runtimeExecutable": "npm",
    
               "runtimeArgs": [
    
                   "run",
    
                   "debug"
    
               ],
    
               "outFiles": [
    
                   "${workspaceFolder}/handler.js"
    
               ],
    
               "port": 9229,
    
               "sourceMaps": true
    
           }
    
       ]
    
    }

    // package.json
    ....
    "scripts": {
        "debug": "SET SLS_DEBUG=* && node --inspect %USERPROFILE%\\AppData\\Roaming\\npm\\node_modules\\serverless\\bin\\serverless offline -s dev"
      }

    If on linux your debug script will be:

    // package.json
    ....
    "scripts": {
        "debug": "export SLS_DEBUG=* && node --inspect /usr/local/bin/serverless offline -s dev"
      }
    

提交回复
热议问题