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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42088007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:14:34  来源:igfitidea点击:

Is there source map support for typescript in node / nodemon?

javascriptnode.jstypescriptnodemon

提问by k0pernikus

I have a node project written in typescript@2.

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

My tsconfig has sourceMapset to trueand the *.map.jsfiles are generated. When I execute my transpiled *.jsJavaScript files via nodeor nodemon, I only see the error messages relative to the jsfile and not to the mapped typescript files; I assume it's completely ignored.

我的 tsconfig 已sourceMap设置为true*.map.js生成文件。当我*.js通过node或执行我转译的JavaScript 文件时nodemon,我只看到与js文件相关的错误消息,而不是与映射的打字稿文件相关的错误消息;我认为它完全被忽略了。

Is sourceMapsupport only intended for browser-support? Or can I use it together with node or nodemon? If the latter, how would I enable it?

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

I want to see runtime errors detected from an executed javascript file relative to the original typescript file.

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

采纳答案by k0pernikus

Install source map support:

安装源地图支持:

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.)

(我也在生产中运行,因为它极大地帮助从发生错误时的日志中查找错误。我没有遇到过很大的性能影响,但您的体验可能会有所不同。)

Add to your tsconfig.json:

添加到您的tsconfig.json

{
   "compilerOptions": {
      "sourceMap": true
   }
}

When running your JavaScript file, add the require parameter:

运行 JavaScript 文件时,添加 require 参数:

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.

但我发现这很乏味,这是具有多个入口点的项目。



Sidenote: mochaalso supports the --require/ -roption, so to have the sourcemap support in mocha you can also call your tests with it, e.g. similar to:

旁注:mocha还支持--require/-r选项,因此要在 mocha 中获得源映射支持,您还可以使用它调用您的测试,例如类似于:

NODE_ENV=test npx mocha --forbid-only --require source-map-support/register --exit --recursive ./path/to/your/tests/

回答by Stephen Paul

I recently got this working in my express app. Steps as follows:

我最近在我的快递应用程序中得到了这个。步骤如下:

Install the required library:

安装所需的库:

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

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

In your entry point (eg app.ts):

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

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

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

In your app.ts, you may also require better logging for errors within promises:

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

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

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

In your tsconfig, under compilerOptions:

在您的tsconfigcompilerOptions

"inlineSourceMap": true

"inlineSourceMap": true

回答by mvermand

I found this npm module which seems to do the trick:

我发现这个 npm 模块似乎可以解决问题:

https://github.com/evanw/node-source-map-support

https://github.com/evanw/node-source-map-support

run npm install source-map-support --saveat the root of your node project and add import 'source-map-support/register'to your main.ts or index.ts file.

npm install source-map-support --save在您的节点项目的根目录运行并添加import 'source-map-support/register'到您的 main.ts 或 index.ts 文件中。

That's it.

而已。

回答by Bruno Grieder

Source map support works perfectly fine with node

源地图支持与节点完美配合

All you need to do is add

您需要做的就是添加

"source-map-support": "0.4.11",

to dependenciesor dev-dependenciesin package.jsonby running

通过运行到dependenciesdev-dependencies进入package.json

npm install --save source-map-support

And in your entry point ts file, simply add at the top

在您的入口点 ts 文件中,只需在顶部添加

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

(note: this is calling nodeJS require- there is no need for source-map-support definition files)

(注意:这是调用 nodeJS require- 不需要 source-map-support 定义文件)