Home > Enterprise >  how to debug a typescript node.js server in vs code
how to debug a typescript node.js server in vs code

Time:11-05

I've been happily debugging a client app using typescript, with vue.js and node.js modules. I now want to rewrite the server side, also in typescript, but the debugger appears to think the code is javascript, not TS:

Uncaught SyntaxError C:\Users\quilk\node-rest-api\app.ts:14
let books: any[] = [];

SyntaxError: Unexpected token ':'

When I hit run/debug, vscode suggests node.js debugger which is what I have tried. There was also an option to install a typescript debug extension but that produced a large number of available plugins , most of which seemed irrelevant.

Can someone explain how I should set up VS code to debug this, perhaps providing a working `launch.json' or whatever else is required?

I have read the MS documention but this appears to be about client-side debugging.

Current launch.json is:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "program": "${workspaceFolder}\\app.ts",
            "outFiles": [
                "${workspaceFolder}/**/*.js"
            ]
        }
    ]
}

and tsconfig.json is:

{

  "compilerOptions": {
    "composite": true,
     "module": "commonjs",
     "sourceMap": true,
    "baseUrl": ".",
    "types": ["node"],
    "paths": {
      "@/*": ["./src/*"]},
    
    "target": "es2016",                                  
    "esModuleInterop": true,                             
    "forceConsistentCasingInFileNames": true,            
    "strict": true,                                      
    "skipLibCheck": true                                 
  }
}

CodePudding user response:

https://github.com/esbuild-kit/tsx/discussions/61#discussioncomment-6883886

        {
            "name": "tsx cur file",
            "type": "node",
            "request": "launch",
            "args": [
                "${relativeFile}"
            ],
            "runtimeArgs": [
                "--require",
                "tsx/suppress-warnings",
                "--loader",
                "tsx",
            ],
            "cwd": "${workspaceFolder}",
            "internalConsoleOptions": "openOnSessionStart"
        },
  • Related