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
It seems that moving the
program
down in theargs
solves the problem. To quote from typestrong.org:So, this configuration works for me at the moment:
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