node/nodemon 中是否有对 typescript 的源映射支持? [英] Is there source map support for typescript in node / nodemon?
问题描述
我有一个用 typescript@2 编写的节点项目.
I have a node project written in typescript@2.
我的 tsconfig 将 sourceMap
设置为 true
并生成 *.map.js
文件.当我通过 node
或 nodemon
执行我转译的 *.js
JavaScript 文件时,我只看到与 js<相关的错误消息/code> 文件而不是映射的打字稿文件;我认为它完全被忽略了.
My tsconfig has sourceMap
set to true
and the *.map.js
files are generated. 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 to the mapped typescript files; I assume it's completely ignored.
sourceMap
支持仅用于浏览器支持吗?或者我可以将它与 node 或 nodemon 一起使用吗?如果是后者,我将如何启用它?
Is sourceMap
support only intended for browser-support? Or can I use it together with node or nodemon? If the latter, how would I enable it?
我想查看从已执行的 javascript 文件中检测到的与原始打字稿文件相关的运行时错误.
I want to see runtime errors detected from an executed javascript file relative to the original typescript file.
推荐答案
安装源地图支持:
npm install source-map-support
(我也在生产环境中运行,因为它极大地帮助从发生错误时的日志中查找错误.我没有遇到很大的性能影响,但您的体验可能会有所不同.)
(I run in in production as well, as it immensely helps finding bugs from the logs of when an error an occurs. I did not experience a large performance impact, yet your experience may be different.)
添加到您的 tsconfig.json
:
{
"compilerOptions": {
"sourceMap": true
}
}
运行 JavaScript 文件时,添加 require 参数:
When running your JavaScript file, add the require parameter:
nodemon -r source-map-support/register dist/pathToJson.js
node -r source-map-support/register dist/pathToJson.js
或者,您添加进入调用:
Alternatively, you add in your entry call:
require('source-map-support').install()
但我发现具有多个入口点的项目很乏味.
yet I find this tedious is projects with multiple entry points.
旁注:mocha 也支持 --require
/-r
选项,以便在 mocha 中获得 sourcemap 支持也可以用它调用你的测试,例如类似于:
Sidenote: mocha also supports the --require
/ -r
option, so to have the sourcemap support in mocha you can also call your tests with it, e.g. similar to:
NODE_ENV=test npx mocha --forbid-only --require source-map-support/register --exit --recursive ./path/to/your/tests/
这篇关于node/nodemon 中是否有对 typescript 的源映射支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!