简体   繁体   English

调试使用 ES6 模块的 JavaScript 代码

[英]Debugging JavaScript code that uses ES6 Modules

TL;DR: How can I access variables/functions/names that are defined in ES Modules from the debugger? TL;DR:如何从调试器访问 ES 模块中定义的变量/函数/名称?

More context: I'm a relatively experienced JavaScript programmer, but new to Modules.更多背景信息:我是一个相对有经验的 JavaScript 程序员,但对模块很陌生。 I've followed the tutorial at MDN here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules .我在这里遵循了 MDN 上的教程: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules They have a good set of examples here: https://github.com/mdn/js-examples/tree/master/modules他们在这里有一组很好的例子: https://github.com/mdn/js-examples/tree/master/modules

In that collection, say in the "basic-modules" example, (live code here: https://mdn.github.io/js-examples/modules/basic-modules/ ) there is, for example, a function called random in the file modules/square.js . In that collection, say in the "basic-modules" example, (live code here: https://mdn.github.io/js-examples/modules/basic-modules/ ) there is, for example, a function called random在文件modules/square.js中。 Suppose I want to execute that function in the debugger, just to try it out, or because it's my code and I want to test/debug it, or I want to demonstrate to another coder what the function does.假设我想在调试器中执行 function,只是为了尝试一下,或者因为它是我的代码并且我想测试/调试它,或者我想向另一个编码器演示 function 的作用。 All the stuff you expect to do in a REPL or debugger.您希望在 REPL 或调试器中做的所有事情。 Is there a way to do that?有没有办法做到这一点? I've tried both the Firefox debugger and the Chrome debugger, with no luck.我已经尝试过 Firefox 调试器和 Chrome 调试器,但都没有成功。

Back in the pre-Modules era, that code would be put into the global namespace (making access easy) or it would be locked up in an IIFE (making access impossible) or maybe in some home-made module system (access depends).回到模块之前的时代,该代码将被放入全局命名空间(使访问变得容易),或者被锁定在 IIFE 中(使访问变得不可能),或者可能在一些自制的模块系统中(访问取决于)。 I am hoping that the new Modules system still allows the debugger access to the names inside modules.我希望新的模块系统仍然允许调试器访问模块内的名称。

Thanks.谢谢。

It says in the docs :在文档中说:

Last but not least, let's make this clear — module features are imported into the scope of a single script — they aren't available in the global scope.最后但同样重要的是,让我们明确一点——模块功能被导入到单个脚本的 scope 中——它们在全局 scope 中不可用。 Therefore, you will only be able to access imported features in the script they are imported into, and you won't be able to access them from the JavaScript console, for example.因此,您将只能在它们被导入的脚本中访问导入的功能,而您将无法从 JavaScript 控制台访问它们。 You'll still get syntax errors shown in the DevTools, but you'll not be able to use some of the debugging techniques you might have expected to use.您仍然会收到 DevTools 中显示的语法错误,但您将无法使用您可能希望使用的一些调试技术。

To take your example from before, you'll need to invoke that function from a scope where it is visible, ie where it's been imported:以你之前的例子为例,你需要从 scope 中调用 function ,它是可见的,即它被导入的地方:

import { random } from 'path/to/square.js'

debugger; // you should be able to invoke random() from here

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

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