gpt4 book ai didi

javascript - 如何以编程方式查找 javascript 语法错误的位置?

转载 作者:搜寻专家 更新时间:2023-10-31 23:49:32 26 4
gpt4 key购买 nike

我正在使用 require 加载 Node.js 模块。这会引发语法错误(错误对象)。

有没有办法获取文件中发生错误的位置?

我可以看到堆栈(下方),但我没有任何位置信息。我知道我可以使用子堆栈的 node-syntax-error ,但是有没有办法从 Javascript 错误对象获取类似的位置信息?

SyntaxError: Unexpected identifier
at Module._compile (module.js:437:25)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
... // the rest is in my code

编辑:

根据下面 Vadim 的评论,是的,这就是我想要做的。我看到那个 Node 抛出了很好的错误。有一个仅包含 abctest.js 文件,您会从 Node 中收到错误消息

ReferenceError: abc is not defined
at Object.<anonymous> (test.js:1:63)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3

现在的问题是,是否有一个 Node API 来解决这个错误( Node module.js 模块使用),这样我就不必 child_process.spawn('node', ['test .js'])?

最佳答案

您可以使用包含 SyntaxError 的文件简单地运行 node 并查看控制台输出。

关于javascript - 如何以编程方式查找 javascript 语法错误的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12036640/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com