vscode 上调试js/ts的方法

对于TS,我用的是ts-node,npm全局安装,

launch.json:

{
    // 使用 IntelliSense 了解相关属性。
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "ts-node Debugger",
            "type": "node",
            "request": "launch",
            "args": [
                "${workspaceRoot}/tsTest/test1.ts" // 入口调试文件
            ],
            "runtimeArgs": ["--nolazy", "-r", "ts-node/register"],
            "sourceMaps": true,
            "cwd": "${workspaceRoot}",
            "protocol": "inspector",
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen",
            "runtimeExecutable": "C:/software/nvm/v16.10.0/node"//你的node安装目录
        }
    ]
}

tsconfig.json:

{
    "compileOnSave": false,
    "compilerOptions": {
        // other compiler options...
        "esModuleInterop": true,
        "module": "ESNext", // "module": "CommonJS" should work too
        "moduleResolution": "Node"
    },
    "include": ["src/*.ts", "tsTest/*.ts"],    //我要对哪些文件生效,这里我只要对tsTest和我src目录下的ts文件应用此配置
    "exclude": ["node_modules"],
    //这里可以不写
    "ts-node": {
        "esm": true,
        "experimentalSpecifierResolution": "node"
    }
}

这里要注意的是,调试ts的话,要删除package.json中的

    "type": "module",
属性,不然会报
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"
这样的错误
posted @ 2023-02-10 10:40  洛晨随风  阅读(276)  评论(0编辑  收藏  举报