Search code examples
node.jstypescriptserverless-framework

How to debug a serverless framework plugin?


I've searched throughout google about this question and I had no success...

I want to work on a serverless plugin fix, but I don't know how to attach the process to debug the code. Currently, I'm using vscode and the plugin was developed using nodejs + typescript.

Does anyone have any tip or article explaining how to do that?


Solution

  • As every other process, that you want to debug, you need to run it and somehow connect the debugger to it.

    You need to remember, that Serverless Framework is written in JS/TS, so it runs in Node.js. So you can debug it quite easily, if you are developing your Lambdas in Node.js, as it's quite common environment.

    How to do it using Jetbrains/Webstorm/IntelliJ

    1. Go to your node_modules directory and find the source code of the plugin, that you want to debug.
    2. In the source code place the breakpoint.
    3. Now Create a new "Run configuration" in IDE for NPM, that should be similar to mine: Run configuration for NPM

    4. Make sure you've chosen the correct package.json!

    1. Now simply start debugging, like you normally do, but choose the run configuration, that you've just created.

    In my example I'm using package script from package.json, but it could be also any other script, that triggers serverless deploy or serverless print in the end.

    And that's it! Breakpoints should be triggered normally, like when you debug your own JS code.