Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to attach debugger to ts-node command in VSCode

How to attach the VSCode debugger to the ts-node command that uses env variables like the below one:

package.json:

{
"runMyScript": "ts-node -r dotenv/config --transpile-only tasks/migration/myScript.ts dotenv_config_path=./local.env"
}

I tried adding flags --respawn --inspect=4000 to the above command with below launch.json, but it didn't work:

launch.json:

{"configurations": [
    {
      "name": "RUN attach to DEV",
      "type": "node",
      "port": 4000,
      "request": "attach",
      "trace": true,
      "skipFiles": ["<node_internals>/**"],
      "restart": true
    },
]}

like image 874
GorvGoyl Avatar asked Dec 13 '25 02:12

GorvGoyl


1 Answers

  • Put this json to .vscode/launch.json or add configurations to your existing configurations
{
  "version": "1.0.0",
  "configurations": [
    {
      "name": "TS-Node",
      "type": "node",
      "request": "launch",
      "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/ts-node",
      "runtimeArgs": [
        "--transpile-only",
        // if you use esm
        "--esm" 
      ],
      "program": "${file}",
      "cwd": "${workspaceRoot}",
      "internalConsoleOptions": "openOnSessionStart",
      "skipFiles": ["<node_internals>/**", "node_modules/**"]
    }
  ]
}

  • Switch to file you want to launch and set breakpoint
  • Enable Run and Debug window on the left side of VSCode and run this configuration called TS-Node
  • Congratulations! You are debugging your typescript code 🥳
like image 75
FreePhoenix888 Avatar answered Dec 15 '25 09:12

FreePhoenix888