简体   繁体   English

使用Visual Studio Code调试“必需”的Node.js依赖项

[英]Use Visual Studio Code to debug Node.js dependency which is “require”d

I want to use VSC to develop some extensions to an existing project. 我想使用VSC为现有项目开发一些扩展。 In particular UglifyJS3. 特别是UglifyJS3。

So I create a test script, in which I call into the module to test and debug my changes. 因此,我创建了一个测试脚本,在其中调用模块来测试和调试更改。 The code there is simply: 那里的代码很简单:

var UglifyJS = require("../tools/node");
var result = UglifyJS.minify(code, ...)

This works while debugging/single-stepping. 在调试/单步执行时有效。 However VSC already fails to resolve the minify function which is exported by UglifyJS. 但是,VSC已经无法解析由UglifyJS导出的minify函数。
I am also not able to simply set breakpoints in the UglifyJS files and have them triggered. 我也不能简单地在UglifyJS文件中设置断点并触发它们。 VSC shows an error on startup about not being able to resolve the breakpoint. VSC在启动时显示有关无法解析断点的错误。

Looking at ../tools/node.js I see code like: ../tools/node.js我看到类似的代码:

var UglifyJS = exports;
require.resolve("../lib/utils.js")
...
require.resolve("../lib/minify.js")
require.resolve("./exports.js")

with exports.js containing exports["minify"] = minify; exports.js包含exports["minify"] = minify; .

Is there anything I have missed? 有什么我想念的吗? How can I make debugging (and optionally IntelliSense) work? 如何使调试(以及可选的IntelliSense)工作?

I found the reason: 我找到了原因:

First clue was that the code showed up in the debugger as <eval> and in a VM<some number> tab. 第一个线索是代码在调试器中显示为<eval>和在VM<some number>选项卡中。 So somehow the code was not actually read from file but dynamically evaluated. 因此,代码实际上并没有从文件中读取,而是经过了动态评估。

What happens is (to a reduced example): 发生了什么(作为简化示例):

var fn = require.resolve("<file>"); // Resolve the filename
var code = fs.readFileSync(fn, "utf8");; // Read the file contents
new Function("exports", code)(exports); // Create a new function containing the files code and execute it

The last bit is a bit more cryptic in code and the new Function was new to me (no pun intended). 最后一点是代码更神秘, new Function对我来说是new Function (无双关语)。 It essentially boils down to an optimized eval with strict-mode compatibility: https://whereswalden.com/2011/01/10/new-es5-strict-mode-support-new-vars-created-by-strict-mode-eval-code-are-local-to-that-code-only/ 它实质上可以归结为具有严格模式兼容性的优化evalhttps : //whereswalden.com/2011/01/10/new-es5-strict-mode-support-new-vars-created-by-strict-mode-评估代码仅限于该代码本地/

Hence: The code executed is actually eval'd code and not the files itself. 因此:执行的代码实际上是评估的代码,而不是文件本身。

Solution: Either live with it or change it upstream to "clean" modules. 解决方案:要么使用它,要么将其更改为上游的“清洁”模块。

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

相关问题 Visual Studio Code 中调试控制台中的 Node.js readline - Node.js readline in debug-console in Visual Studio Code 如何在 Visual Studio Code 中调试子 Node.JS 进程? - How to debug child Node.JS process in Visual Studio Code? 在Visual Studio 2017中调试node.js - Debug node.js in Visual Studio 2017 使用Visual Studio Code调试节点js - Debug node js with Visual Studio Code 如何在不转译代码的情况下在 Visual Studio Code 中使用 ES6 模块调试 Node.JS? - How to debug Node.JS with ES6 modules in Visual Studio Code without transpiling the code? 未捕获的 ReferenceError: require 未定义。 Visual Studio 中的 Node.js - Uncaught ReferenceError: require is not defined. Node.js in Visual Studio TypeScript,Node.js,需求和Visual Studio 2013中的模块 - TypeScript, Node.js, require and module in Visual Studio 2013 在Visual Studio Code中运行Node.js,将python脚本作为子进程调用并对其进行调试 - In Visual Studio Code run Node.js, call python script as child-process and debug it 我可以在Node.js应用程序中使用ES6模块并在Visual Studio中对其进行调试吗? - Can I use ES6 modules in a Node.js application and debug it in Visual Studio? Node.JS Winston日志记录,Visual Studio调试控制台无法打印 - Node.JS Winston logging, Visual Studio Debug Console Not Print
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM