简体   繁体   English

在节点应用程序中遇到断点时如何使VSCode打开“src”的原始TS文件

[英]How to make VSCode open original TS file of “src” when hitting a breakpoint in node app

I have a CLI node app that I am trying to debug with VSCode.我有一个 CLI 节点应用程序,我正在尝试使用 VSCode 进行调试。 It works pretty well, however when hitting a breakpoint, VSCode opens a new code view from the source map file instead of the actual TS file located in my "src" folder.它工作得很好,但是当遇到断点时,VSCode 从源 map 文件而不是位于我的“src”文件夹中的实际 TS 文件打开一个新的代码视图。 This is kind of annoying.这有点烦人。 When I run some JS code in a browser using VSCode as a debugger, VSCode opens the actual TS file as expected.当我使用 VSCode 作为调试器在浏览器中运行一些 JS 代码时,VSCode 会按预期打开实际的 TS 文件。 How do I get this behavior also with node?我如何通过节点获得这种行为?

在此处输入图像描述

launch.json : launch.json

{
    // 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": "pwa-node",
            "request": "launch",
            "name": "Node",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "program": "${workspaceFolder}/bin/js/index.js",
            "console": "integratedTerminal",
            "cwd": "${workspaceFolder}/bin/js",
            "args": [
                "authorize"
            ]
        }
    ]
}

tsconfig.json : tsconfig.json

{
    "compilerOptions": {
        "baseUrl": "./src",
        "moduleResolution": "node",
        "module": "ES2020",
        "target": "ES2018",
        "allowSyntheticDefaultImports": true,
        "jsx": "react",
        "strict": true,
        "strictPropertyInitialization": true,
        "noEmitOnError": true,
        "noImplicitAny": true,
        "removeComments": true,
        "preserveConstEnums": true,
        "outDir": "./bin",
        "sourceMap": true,
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true
    },
    "include": [
        "src/**/*"
    ],
    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ]
}

webpack.config.json : webpack.config.json

const path = require("path");
const webpack = require("webpack");
const { merge } = require('webpack-merge');

const commonConfig = {
    target: 'node',
    entry: "./src/Startup.ts",
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                use: [{
                    loader: "ts-loader"
                }]
            }
        ]
    },
    resolve: {
        extensions: [".ts", ".tsx", ".js", ".jsx"],
        modules: ["./src", "node_modules"]
    },
    output: {
        path: path.resolve(__dirname, "./bin/js/"),
        filename: "index.js",
    },
    plugins: [
        new webpack.DefinePlugin({ "global.GENTLY": false })
    ],
    externals: {
        'cliui': 'commonjs2 cliui',
        'y18n': 'commonjs2 y18n',
        'yargs-parser': 'commonjs2 yargs-parser',
    }
}

const developmentConfig = {
    mode: 'development',
    devtool: 'source-map',
    stats: {
        warnings: false
    }
}

const productionConfig = {
    mode: 'production'
}

module.exports = (env, argv) => {
    switch(argv.mode) {
      case 'development':
        return merge(commonConfig, developmentConfig);
      case 'production':
        return merge(commonConfig, productionConfig);
      default:
        throw new Error(`Configuration '${argv.mode}' does not exists.`);
    }
}

The TS file paths generated by Webpack in the source map files are using the webpack protocol that VSCode did not understand.源map文件中Webpack生成的TS文件路径使用的是VSCode不理解的webpack协议。 I solved it by adding this parameter in my webpack.config.js so the source map contains absolute paths to the TypeScript files instead:我通过在我的webpack.config.js中添加这个参数来解决它,所以源 map 包含 TypeScript 文件的绝对路径:

const commonConfig = {
    output: {
        devtoolModuleFilenameTemplate: "[absolute-resource-path]",
    },
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ./src/app.ts 中的未知文件扩展名“.ts” - Unknown file extension ".ts" in ./src/app.ts 无法运行我的 Node.js Typescript 项目 TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /app/src/App.ts - Can't run my Node.js Typescript project TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /app/src/App.ts 节点检查器未达到实习生测试的断点 - Node Inspector not hitting breakpoint for Intern test 如何使VSCode运行“ npm start”之类的节点应用程序? - How can I make VSCode run a node app like “npm start”? 如何从原型文件生成_grpc_pb.d.ts以便与Node app中的gRPC一起使用? - How to generate the _grpc_pb.d.ts from a proto file for use with gRPC in a Node app? 如何在开发模式下使用 TypeScript 和 ts-node 在 NodeJS 应用程序中导入文本文件? - How to import a text file in a NodeJS app with TypeScript and ts-node in dev mode? 导出不适用于pm2中部署的ts文件节点应用 - EXPORT not working in ts file node app deployed in pm2 如何在node.js项目中将.d.ts类型本地用于vscode intellisense? - How to use the .d.ts typings locally for vscode intellisense in a node.js project? 在TypeScript文件中使用node.d.ts时发生编译错误 - Compilation error when using node.d.ts in TypeScript file 错误:点击 Docker 容器化 Node.js 应用程序端点时连接 ECONNREFUSED 0.0.0.0:8000 - Error: connect ECONNREFUSED 0.0.0.0:8000 when hitting Docker containerised Node.js app endpoint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM