当前位置: 首页 > 知识库问答 >
问题:

如何在visual studio代码中调试typescript文件

姬歌者
2023-03-14

使用0.3版本的视觉工作室代码,我不知道如何启用源地图和调试ts文件

我遇到以下错误无法启动程序'/Projects/app server/server。ts';启用源地图可能会有所帮助

如何启用源地图和打字稿调试。源地图在我的

tsconfig。json

{
    "compilerOptions": {
        "target": "ES5",
        "module": "commonjs",
        "sourceMap": true
    }
}

launch.json

{
    "version": "0.1.0",
    // List of configurations. Add new configurations or edit existing ones.  
    // ONLY "node" and "mono" are supported, change "type" to switch.
    "configurations": [
        {
            // Name of configuration; appears in the launch configuration drop down menu.
            "name": "Launch server.ts",
            // Type of configuration. Possible values: "node", "mono".
            "type": "node",
            // Workspace relative or absolute path to the program.
            "program": "server.ts",
            // Automatically stop program after launch.
            "stopOnEntry": true,
            // Command line arguments passed to the program.
            "args": [],
            // Workspace relative or absolute path to the working directory of the program being debugged. Default is the current workspace.
            "cwd": ".",
            // Workspace relative or absolute path to the runtime executable to be used. Default is the runtime executable on the PATH.
            "runtimeExecutable": null,
            // Environment variables passed to the program.
            "env": { }
        }, 
        {
            "name": "Attach",
            "type": "node",
            // TCP/IP address. Default is "localhost".
            "address": "localhost",
            // Port to attach to.
            "port": 5858
        }
    ]
}

共有3个答案

徐嘉勋
2023-03-14

这就是我从2017年11月开始使用最新TS和VsCode的工作原理

以下配置将帮助您启动服务器并调试VS代码中的TS

这就是我的配置。json看起来像:

{
    "compilerOptions": {
        "declaration": false,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "lib": ["es2017", "dom"],
        "module": "commonjs",
        "moduleResolution": "node",
        "outDir": "../build",
        "sourceMap": true,
        "target": "es2016",
        "typeRoots": [
            "../node_modules/@types"
        ]
    },
    "include": [
        "**/*.ts"
    ],
    "exclude": [
        "../node_modules",
        "../*.js"
    ]
}

这就是我的目录结构:

如果你注意,你会看到我的src文件夹和构建文件夹(包含结果转播的JS和映射文件)并排存在,这真的有助于我维护一个逻辑目录结构。

好的,现在是启动配置:

{
            "type": "node",
            "request": "launch",
            "name": "Start and Debug",
            "preLaunchTask": "nb-tsc-watch",
            "timeout": 10000,
            "program": "${workspaceFolder}/backend/src/app.ts",
            "restart": true,
            "cwd": "${workspaceRoot}",
            "outFiles": [
                "${workspaceFolder}/backend//build/**/*.js"
            ],
            "sourceMaps": true
        }

你可以使用任何你想使用的任务,甚至可以跳过它。如果跳过它,就必须手动将TS传输到JS。

这就是我在任务中所做的nb tsc watch

{
            "label": "nb-tsc-watch",
            "type": "typescript",
            "tsconfig": "backend/src/tsconfig.json",
            "option": "watch",
            "problemMatcher": [
                "$tsc-watch"
            ]
        }
宋鸿
2023-03-14

我觉得随着版本的发布,它变得越来越简单。。。

我已经安装了ts节点(https://github.com/TypeStrong/ts-node),所以我的配置文件非常简单。

安装ts节点使用npm安装ts节点——将dev保存在项目文件夹中——感谢评论中的Hrafnkell

{
        "name": "Launch index.ts",
        "type": "node",
        "request": "launch",
        "runtimeArgs": [
            "-r",
            "ts-node/register"
        ],
        "args": [
            "${workspaceFolder}/src/index.ts"
        ]
}

有两件事值得注意:

  • runtimeArgs-传递给节点以注册ts节点以处理TypeScript文件

这样,您就不需要将TS编译成JS,甚至可以让用TS编写的模块尚未编译成JS。

冯浩旷
2023-03-14

这个配置对我来说很好:

|-- .vscode
    |----- launch.json
|-- bin
    |----- app.js
    |----- app.js.map
|-- src
    |----- app.ts
|-- node_modules
    |-- [..]
|-- tsconfig.json
|-- [...]

想法是在src文件夹下编译类型脚本,并将其放在bin文件夹下。

激活sourceMap选项很重要。

{
    "compilerOptions": {
        "emitDecoratorMetadata": true,
        "module": "commonjs",
        "target": "ES5",
        "outDir": "bin",
        "rootDir": "src",
        "sourceMap": true
    }
}

====编辑====

这是我目前在Visual Studio Code v1中使用的配置:

{
    "version": "0.2.0",
    "configurations": [
        {
            "args": [],
            "cwd": "${workspaceRoot}",
            "env": {
                "NODE_ENV": "development"
            },
            "externalConsole": false,
            "name": "DEBUG",
            "outDir": "${workspaceRoot}/bin",
            "preLaunchTask": "compile",
            "program": "${workspaceRoot}/src/app.ts",
            "request": "launch",
            "runtimeArgs": [
                "--nolazy"
            ],
            "runtimeExecutable": null,
            "sourceMaps": true,
            "stopOnEntry": false,
            "type": "node"
        },
        {
            "name": "Attach",
            "type": "node",
            "request": "attach",
            "port": 5858
        }
    ]
}

注意:如果您将任何任务运行程序用作gulp,则键preLaunchTask非常有用,因为IDE能够按名称检测其任务。

  1. 编译您的ts(输入终端rm-r bin/; tsc或执行编译任务)
  2. 在可视化代码播放启动类型(我们的配置名称)
  3. 享受!
 类似资料:
  • 我想缩进VisualStudio代码中的特定代码段。 我读了如何在Visual Studio Code中格式化代码?这提供了缩进整个代码的快捷方式,但在选择特定代码部分时不起作用。 在我的代码中选择了一些行后,我尝试了CtrlShiftF,但是整个文件是缩进的。我在Windows上使用Visual Studio Code Insider 1.8.0。我怎么能做到呢?

  • 我想比较visual studio中的两个文件(包含存储过程的文件),所以我想忽略文件中所有的差异。 我打开命令窗口,使用但它也考虑了案例差异 我已经看过这个问题和答案了

  • 了解如何在 Dreamweaver 中清除代码、检查浏览器兼容性、验证 XML 文档并使页面符合 XHTML 规范。 清理代码 您可以自动删除空标签,合并嵌套 font 标签,以及通过其它方法改善杂乱或难以辨识的 HTML 或 XHTML 代码。 有关如何清理从 Microsoft Word 文档生成的 HTML 的信息,请参阅打开和编辑现有文档。 在打开的文档中,选择“工具”>“清理 HTML”

  • 例如,在node.js源文件的顶部: …或未使用的局部变量等。

  • 我遵循以下指示https://code.visualstudio.com/docs/setup/mac但我没有成功。这就是我进去时发生的事情 我得到了以下信息: 我还查找了以下问题:如何在OSX上从命令行打开Visual Studio代码?,这没用。 我在VSCode中只有一个终端选项,zsh。

  • 下面通过一个简单的例子来了解一下 Eclipse 调试程序的方法。 上述代码完成的主要功能是如果 i 值满足小于或等于 5 的条件,就一直执行输出语句。可以看到 for 关键字后面的小括号中有三个表达式,第一个表达式 的作用是定义一个 int 类型的变量并赋初值为 0,第二个表达式 说明 i 要满足的条件是小于或等于 5,第三个表达式 的意思是程序每执行一次 i 加 1。 对初学者来说,可能对这几