Typescript-restful-starter icon indicating copy to clipboard operation
Typescript-restful-starter copied to clipboard

Debug using Devtools for Node

Open MattijsE opened this issue 7 years ago • 4 comments
trafficstars

Is it possible to add support for debugging with the 'npm run dev' command? I can't see to make it work using --inspect or --inspect -r ts-node/register Index.ts.

Maybe it's some issue with the combination of nodemon and ts-node. Debugging support make it so easy to debug issues.

MattijsE avatar Jun 21 '18 21:06 MattijsE

Hi @MattijsE ,

I was looking for this too and just found a solution that works for me ;-)

You can do it with npm run dev:inspect adding this to your scripts section of package.json:

"dev:inspect": "cross-env NODE_ENV=DEVELOPMENT ./node_modules/.bin/nodemon --watch '**/*.ts' --exec 'node --inspect -r ts-node/register' Index.ts --ignore 'test/*.ts'",

As you can see, you must replace ts-node with node to use -r ts-node/register option.

After you run this new script, you can open chrome://inspect/#devices and click in Open dedicated DevTools for Node link to access to the DevTools where you will find your sources and you will be able to set breakpoints in files tagged with [sm] (from source map).

Best regards, Fernando.

fsanzv avatar Jul 09 '18 16:07 fsanzv

Hi @fsanzv, If you found a solution for this, you can feel safe to send a pull request. :+1:

camesine avatar Jul 10 '18 02:07 camesine

I think this is working for me in WebStorm:

    "dev": "cross-env NODE_ENV=DEV ./node_modules/.bin/nodemon --watch '**/*.ts' --exec 'node %NODE_DEBUG_OPTION% -r ts-node/register' Index.ts --ignore 'test/*.ts'",
    "test": "cross-env NODE_ENV=TEST ./node_modules/mocha/bin/mocha --require ts-node/register --timeout 20000 test/*.ts --exit -- %NODE_DEBUG_OPTION%",

The %NODE_DEBUG_OPTION% just puts --inspect or --inspect-brk in if you select the "Debug" button instead of run, but you could easily make "dev:inspect" and "test:inspect" npm tasks as @fsanzv did above as well.

mandreko avatar Jul 10 '18 13:07 mandreko

Algo got it working in VSCode. Create a launch.json file in your project's .vscode folder and paste this:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Debug Typescript Node Cluster",
      "sourceMaps": true,
      "program": "${workspaceFolder}/Index.ts",
      "outFiles": [
        "${workspaceFolder}/dist/**/*.js"
      ],
      "autoAttachChildProcesses": true,
      "preLaunchTask": "npm: build",
    }
  ]
}

Now you can debug using VSCode to set breakpoints and watch variables.

fsanzv avatar Jul 10 '18 16:07 fsanzv