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

4

11

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?

Maratha answered 25/6, 2018 at 23:1 Comment(1)
This question is not about debugging but changing the port. Misleading question titleVeridical
M
5

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

custom:
    serverless-offline:   ## add this two lines
        port: 4000        ## bellow "custom:" line
Maratha answered 25/6, 2018 at 23:39 Comment(3)
How this solves the problem? you don't need to add a launch.json file?Aurthur
Looks like the parameter is now httpPort (see docs)Jarvey
The question has misleading title and the user only wants to change the default portVeridical
I
22

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"
  }
Icsh answered 31/8, 2018 at 11:8 Comment(0)
M
5

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

custom:
    serverless-offline:   ## add this two lines
        port: 4000        ## bellow "custom:" line
Maratha answered 25/6, 2018 at 23:39 Comment(3)
How this solves the problem? you don't need to add a launch.json file?Aurthur
Looks like the parameter is now httpPort (see docs)Jarvey
The question has misleading title and the user only wants to change the default portVeridical
R
0

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"
  },
Relevant answered 26/5, 2022 at 7:1 Comment(0)
V
0

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
}
Veridical answered 29/11, 2023 at 13:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.