How to debug Serverless Offline in Visual Studio Code using another port?

21.9k Views Asked by At

I have two Serverless Offline "servers" which I need to run locally at same time.

So I need to change the port of one of the servers.

I run a server using Visual Studio Code debugger. The configs of the servers are in launch.json files.

How can I change the port of a Serverless Offline application so that I can run it in parallel with another Serverless Offline application using VS Code debugger?

4

There are 4 best solutions below

3
On BEST ANSWER

Solved by adding the following lines to the serverless.yml file:

custom:
    serverless-offline:   ## add this two lines
        port: 4000        ## bellow "custom:" line
0
On

Port can be changed by using the below script in package.json and then use npm run start

 "scripts": {   
    "start": "cross-env CONFIG_SOURCE=dev sls offline start --stage=local --reloadHandler --httpPort=4000",
    // ... other scripts
}
0
On

I'm using a Linux system, here is what my launch.json and package.json files look like.

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "pwa-node",
            "request": "launch",
            "name": "Debug Serverless",
            "cwd": "${workspaceFolder}",
            "runtimeExecutable": "npm",
            "runtimeArgs": [
                "run",
                "debug"
            ],
           "sourceMaps": true,
           "attachSimplePort": 9229
        }
    ]
}

package.json

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "debug": "node --inspect node_modules/serverless/bin/serverless offline -s dev"
  },
0
On

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"
  }