Search code examples
node.jstypescriptvisual-studio-codets-node

VSCode ts-node/register launches old compiled JS file instead of the specified TS file


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 Files\nodejs\node.exe -r ts-node/register ...\bin\index.js

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

C:\Program Files\nodejs\node.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?


Solution

  • 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
            }
        ]
    }