Cli: Any way to debug functions?

Created on 16 Jul 2019  Â·  16Comments  Â·  Source: netlify/cli

Not a bug or feature request but an enquiry:

What context do javascript lambda functions run in when using netlify dev? Is there any way to pass the --inspect flag so a debugger can be connected like a standard node app?

Thanks, and great work on netlify dev.

docs dx feature

Most helpful comment

I haven't looked at how netlify-cli works, but I'd assume the following will do the job:

node --inspect node_modules/.bin/netlify-cli dev

All that's really needed to make this work is to pass the --inspect flag to the node process which executes the actual function code (presumably the same node process as the netlify-cli command itself).

If the above works, all that @sw-yx and the team would really need to do is provide their own flag (eg, --node-flag-inspect) which would pass the --inspect flag to the node process which executes the functions - this would only serve to clean up the syntax.

All 16 comments

this is interesting bc nobody’s asked for this before but it seems so natural. guess we’re all used to console.log debugging 😂

Raes is probably better at answering the —inspect flag but in general netlify dev is meant to make functions super easy to debug. its not much different from running a node server locally with env variables injected from your build settings. perhaps if you gave more insight into your problem we can suggest debugging steps

Not sure if this is what @fineline needs, but for me, being able to attach a local instance of the chrome debugger so that I can step through a lambda function is in orders of magnitude more helpful than just console.log() statements.

what do we need to do on our end to make that happen? never done this before. i guess node doesnt just do this by default? 😂

I haven't looked at how netlify-cli works, but I'd assume the following will do the job:

node --inspect node_modules/.bin/netlify-cli dev

All that's really needed to make this work is to pass the --inspect flag to the node process which executes the actual function code (presumably the same node process as the netlify-cli command itself).

If the above works, all that @sw-yx and the team would really need to do is provide their own flag (eg, --node-flag-inspect) which would pass the --inspect flag to the node process which executes the functions - this would only serve to clean up the syntax.

@8eecf0d2 I tried node --inspect node_modules/netlify-cli/bin/run dev, but it doesn't seem to work. I think you might be right about the --inspect on the child lambda process that netlify dev is making.

Edit: Actually, I can see that Chrome debugger is able to attach to localhost:34567 (the lambda server) with node --inspect node_modules/netlify-cli/bin/run dev. I just need to figure out how to use it properly. I can't really see my source code. I use the webstorm IDE too, so ideally I'll get it working in there.

I have some more progress. I'm able to hit breakpoints in the built js files in Webstorm and Chrome Debugger. Note that the debugger has to point to port 9229 and not the port the lambdas are served from. https://nodejs.org/de/docs/guides/debugging-getting-started/.

I think the next step is to figure out how to generate source-maps with netlify-lambda so I can hit ts breakpoints.

npx --node-arg=--inspect netlify dev also appears to work

Okay I got it working with TypeScript. For anyone wondering:

    "build:lambda": "netlify-lambda build src/lambda --config ./webpack.config.dev.js",
    "dev": "npx --node-arg=--inspect netlify dev"

in the webpack.config.dev.js

module.exports = {
    devtool: 'inline-source-map',
    optimization: { minimize: false }
};

run dev script and attach a debugger to localhost:9229

@sudall just doing node --inspect node_modules/.bin/netlify dev worked for me. Needed to do yarn add -D netlify-cli locally, however.

Any updates on this?

Looks like node --inspect node_modules/.bin/netlify-cli dev is the best workaround at the moment. I feel using this command with chrome debugger attached(about://inspect) is fairly convenient and I'm personally using indium for code inspection. Note that you should not use netlify-lambda if you don't want extra settings. You can just create functions ntl functions:create fun-name and debug by executing node with --inspect. Looks like netlify handles the rest of hassle jobs. Wonder how that works though.

Just an update to this, today I discovered the NODE_OPTIONS environment variable which allows you to pass flags to the node binary and looks something like:

NODE_OPTIONS=--inspect netlify dev

I think this is a much nicer experience, so nice in fact that if this was added to the docs I think this issue could be closed.

Worked for me in Nuxt.js project on Mac OS:

node --inspect-brk=9229 /usr/local/bin/netlify dev

Just an update to this, today I discovered the NODE_OPTIONS environment variable which allows you to pass flags to the node binary and looks something like:

NODE_OPTIONS=--inspect netlify dev

I think this is a much nicer experience, so nice in fact that if this was added to the docs I think this issue could be closed.

That didn't work for me, it returned:

â—ˆ Functions server is listening on 59515
Starting inspector on 127.0.0.1:9229 failed: address already in use
â—ˆ "yarn start" exited with code 12. Shutting down Netlify Dev server

Even though no other node instance is running

You can do this in VS Code by adding a launch file. In order for VS Code to attach a debugger, the program that is executed must be relative to the local workspace, so first install netlify dev locally within your project as a dev dependency:

npm install netlify-cli --save-dev

Then you can add the following launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "program": "${workspaceFolder}\\node_modules\\.bin\\netlify",
            "args": ["dev"]
        }
    ]
}

Other Threads:

I got it working in VS Code using the following:

package.json

{
  ...
  "scripts": {
    "debug": "netlify dev --inspect",
    ...
  },
  ...
}

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch Node",
      "type": "pwa-node",
      "request": "launch",
      "runtimeArgs": ["run-script", "debug"],
      "runtimeExecutable": "npm",
      "skipFiles": ["<node_internals>/**"]
    }
  ]
}

Using [email protected]

Was this page helpful?
0 / 5 - 0 ratings

Related issues

janbaer picture janbaer  Â·  4Comments

cco3 picture cco3  Â·  4Comments

karl-cardenas-coding picture karl-cardenas-coding  Â·  4Comments

lukasluecke picture lukasluecke  Â·  3Comments

KazakovKirill picture KazakovKirill  Â·  4Comments