skip to Main Content

I have the following launch.json for my TypeScript project in Visual Studio Code:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch task",
      "type": "node",
      "request": "launch",
      "program": "${workspaceFolder}/src/index.ts",
      "cwd": "${workspaceFolder}/src",
      "runtimeArgs": [
        "-r",
        "ts-node/register"
      ],
      "env": {
        "NODE_ENV": "development"
      },
      "sourceMaps": true
    }
  ]
}

This is used to debug my Azure DevOps pipeline task extension. I have some NPM scripts to build and package the extension which outputs the compiled JavaScript files in the ./bin folder as per the tsconfig.json:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "sourceMap": true,
    "outDir": "./bin",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  }
}

I have experienced that after packaging and then making some more code changes, when I run the Launch task debug configuration above, it runs the generated index.js from the bin folder rather than the src/index.ts. This causes some weird debugging experience as the TypeScript source doesn’t match the JavaScript being debugged. From the debug console:

C:Program Filesnodejsnode.exe -r ts-node/register …binindex.js

If I delete the bin folder it launches the expected index.ts file:

C:Program Filesnodejsnode.exe -r ts-node/register .index.ts

Is there some configuration/CLI option to force ts-node to clean/recompile the source to avoid accidentally debugging an old version of the code?

2

Answers


  1. Chosen as BEST ANSWER

    It seems that moving the program down in the args solves the problem. To quote from typestrong.org:

    [...] and move the program to the args list (so VS Code doesn't look for outFiles)

    So, this configuration works for me at the moment:

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Launch task",
                "type": "node",
                "request": "launch",
                "cwd": "${workspaceFolder}/src",
                "runtimeArgs": [
                    "-r",
                    "ts-node/register"
                ],
                "args": [
                    "${workspaceFolder}/src/index.ts"
                ],
                "env": {
                    "NODE_ENV": "development"
                },
                "sourceMaps": true
            }
        ]
    }
    

  2. ts-node prefers complied files by default

    Use https://typestrong.org/ts-node/docs/options/#prefertsexts to prefer TS files


    Or try using https://github.com/esbuild-kit/tsx , it "just works" in most cases.
    The script for watcher (relaunching on changes) would be tsx watch (as you are running index of cwd)
    My task configuration is

          {
            "name": "backend",
            "type": "node",
            "request": "launch",
            "runtimeExecutable": "yarn",
            "runtimeArgs": [
              "dev" // script name, basically "yarn <implied>run dev"
            ],
            "localRoot": "${workspaceFolder}/packages/server",
            "presentation": {
              "hidden": true,
              "group": "partial",
              "order": 2
            },
            "console": "integratedTerminal"
          },
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search