You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用npm link调试TypeScript时断点无法命中.ts文件的问题

问题:npm link关联模块后VS Code断点无法命中TypeScript源文件

我需要实现一个基础功能:让断点能正常工作,既能在当前开发的代码中使用,也能在通过npm link关联的模块上使用。

  • 已将第一个模块通过npm link关联到第二个模块,两个模块均采用src+dist的基础文件夹结构
  • 两者都配置了类似的tsconfig.json生成source map,尝试过内联source map、嵌入TypeScript source map等多种方式
  • 无论如何配置,在modules/@myorg/firstmodule文件夹的.ts文件上设置断点时,最多只能命中对应的.js文件
  • 尝试过修改package.json、tsconfig、VS Code启动配置,以及ts-node、tsx、纯tsc搭配node等运行方式,均未解决问题

被关联模块的package.json片段

我原本在./src/config/config.ts这类文件上设置断点,添加files数组是想让dist识别子文件夹的映射文件,但未生效:

"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"typings": "./dist/index.d.ts",
"files": [
  "./dist/**/*"
],

注:保留visual-studio-code标签,问题可能是VS Code特有的,所有操作均在VS Code中进行,对其他编辑器配置不熟悉。已确认VS Code本身可正常追踪代码。

第一个模块的tsconfig.json片段

{
  "include": ["./src/**/*"],
  "compilerOptions": {    
    /* Visit https://aka.ms/tsconfig to read more about this file */
    /* Projects */
    "composite": true,                                  /* Enable constraints that allow a TypeScript project to be used with project references. */
    /* Language and Environment */
    "target": "ESNext",                                  /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
    /* Modules */
    "module": "CommonJS",                                  /* Specify what module code is generated. */    
    "sourceRoot": "",
    "rootDir": "src",                                   /* Specify the root folder within your source files. */
    "outDir": "dist",                                   /* Specify an output folder for all emitted files. */
    //"moduleResolution": "node",                          /* Specify how TypeScript looks up a file from a given module specifier. */     
    /* Emit */        
    //"inlineSourceMap": true,
    "sourceMap": true,
    "inlineSources": true,
    "declaration": true,                                 /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
    "declarationMap": true,
    "declarationDir": "dist",
    /* Interop Constraints */
    "allowSyntheticDefaultImports": true,                /* Allow 'import x from y' when a module doesn't have a default export. */
    "esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
    "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */
    /* Type Checking */
    "strict": true,                                      /* Enable all strict type-checking options. */
    "alwaysStrict": true,                                /* Ensure 'use strict' is always emitted. */
  }
}

VS Code的launch.json片段

{
      "name": "tsc",
      "type": "node",
      "request": "launch",
      "runtimeArgs": ["--preserve-symlinks"],
      "program": "${workspaceFolder}/src/tests.ts",
      "preLaunchTask": "tsc: build - tsconfig.json",
      "sourceMaps": true,
      "resolveSourceMapLocations": [
        "${workspaceFolder}/**/*",
        "${workspaceFolder}/node_modules/@korodarn/amp/dist/**/*"
      ]      
  }

内容的提问来源于stack exchange,提问作者Matthew Swaringen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 14:47:01