简体   繁体   English

如何调试从 CoffeeScript 生成的 Node.js/JavaScript 代码?

[英]How to debug Node.js/JavaScript code generated from CoffeeScript?

I'm using CoffeeScript with Node.js.我在 Node.js 中使用 CoffeeScript。 I want to be able to debug JavaScript errors/exceptions that pop up, but the line numbers for those errors are from the generated JavaScript code, not from the CoffeeScript source, so I'm having trouble figuring out where in the CoffeeScript the error actually is.我希望能够调试弹出的 JavaScript 错误/异常,但是这些错误的行号来自生成的 JavaScript 代码,而不是来自 CoffeeScript 源代码,所以我无法弄清楚 CoffeeScript 中的错误实际上在哪里是。

Is there anyway I can debug this?无论如何我可以调试这个吗? I have the latest version of node.js, version 0.10 .我有最新版本的 node.js,版本0.10

The CoffeeScript compiler supports source maps , so there is a mapping from JavaScript to Coffeescript with the information you need to know. CoffeeScript 编译器支持 source maps ,因此有一个从 JavaScript 到 Coffeescript 的映射,其中包含您需要知道的信息。 For your server-side project I think that Jet Brains uses this map for debugging .对于您的服务器端项目,我认为Jet Brains 使用此映射进行调试 On the browser side Chrome seems to support it .在浏览器方面Chrome 似乎支持它 I'm not sure if one of the tools works for you, but if not I think CoffeeScript and source map are the words you should look for.我不确定其中一种工具是否适合您,但如果不是,我认为 CoffeeScript 和 source map 是您应该寻找的词。 Good luck!祝你好运!

I created a tool for LiveScript, which works very close to CoffeeScript so you can modify it according to your needs: https://github.com/ceremcem/debug-ls我为 LiveScript 创建了一个工具,它的工作原理与 CoffeeScript 非常接近,因此您可以根据需要对其进行修改: https : //github.com/ceremcem/debug-ls

Briefly:简要地:

  • Use your favourite bundler (I use Rollup in this case) to generate one bundle with sourcemaps from your main script.使用您最喜欢的打包器(在本例中我使用 Rollup)从您的主脚本生成一个包含源映射的包。

  • Run your script with the following command:使用以下命令运行您的脚本:

     node --enable-source-maps --inspect-brk your-bundle.js
  • Go to chrome://inspect/#devices address in Chrome在 Chrome 中转到chrome://inspect/#devices地址

  • Find your process that is listed under "Remote Target" section找到“远程目标”部分下列出的进程

  • Click "inspect" button点击“检查”按钮

because sourcemap, coffee just like js.因为 sourcemap、coffee 就像 js。 so we can debug coffee in vscode.这样我们就可以在vscode中调试coffee了。 anyway here is my launch.json:无论如何这是我的launch.json:

{
    "version": "0.2.0",
    "configurations": [{
        "type": "node",
        "request": "launch",
        "name": "Launch Program",
        "skipFiles": [
            "<node_internals>/**"
        ],
        "program": "${file}", //important, make sure debug current file
        "outFiles": [
            "${workspaceFolder}/dist/api/api.js" //important, where to find sourcemap js file
        ]
    }]
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM