node/nodemon 中是否有对 typescript 的源映射支持?

新手上路,请多包涵

我有一个用 typescript@2 编写的节点项目。

我的 tsconfig 有 sourceMap 设置为 true 并生成 *.map.js 文件。 When I execute my transpiled *.js JavaScript files via node or nodemon , I only see the error messages relative to the js file and not到映射的打字稿文件;我认为它被完全忽略了。

sourceMap 支持仅用于浏览器支持吗?或者我可以将它与 node 或 nodemon 一起使用吗?如果是后者,我将如何启用它?

我想查看从已执行的 javascript 文件中检测到的相对于原始打字稿文件的运行时错误。

原文由 k0pernikus 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 372
2 个回答

这里的答案对于 v12.12.0 之前的节点版本是正确的,它添加了(实验性的) --enable-source-maps 标志。启用后,源映射将应用于堆栈跟踪而无需额外的依赖。如 本文 所示,它具有略微不同但可能有益的行为,包括生成的 .js 文件位置和源文件位置。例如:

 Error: not found
    at Object.<anonymous> (/Users/bencoe/oss/source-map-testing/test.js:29:7)
        -> /Users/bencoe/oss/source-map-testing/test.ts:13:7

原文由 Trevor Robinson 发布,翻译遵循 CC BY-SA 4.0 许可协议

🚩 对于 v12.12 之后的 Node 版本,有一个 更简单更好的解决方案

我最近在我的 express 应用程序中使用了它。步骤如下:

安装所需的库:

npm install --save-dev source-map-support

在您的入口点(例如 app.ts ):

require('source-map-support').install();

在您的 app.ts 中,您可能还需要更好地记录承诺中的错误:

process.on('unhandledRejection', console.log);

在你的 tsconfigcompilerOptions 下:

"inlineSourceMap": true

原文由 Stephen Paul 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题