3

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

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

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

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

编辑:

根据下面瓦迪姆的评论,是的,这就是我想做的事情。我看到那个节点抛出了很好的错误。拥有一个test.js仅包含abc您的文件的文件会从节点收到错误消息

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

现在的问题是,是否有一个节点 API 可以得到这个错误(节点 module.js 模块使用),这样我就不必了child_process.spawn('node', ['test.js'])

4

4 回答 4

3

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

于 2012-08-20T11:39:15.467 回答
0

错误堆栈

错误具有存储堆栈跟踪的“堆栈”属性。

try {
    throw new Error("with some message");
}
catch(err) {
    // print error's stack trace to stder
    console.error(err.stack);
}

在名为“/home/myusername/test.js”的文件上运行它

node /home/myusername/test.js

将输出以下内容

Error: with some message
    at Object.<anonymous> (/home/myusername/test.js:2:11)
    at Module._compile (module.js:449:26)
    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.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)
于 2013-04-19T01:39:17.507 回答
0

@laconbass 答案对我有用。

err.stack

我尝试了一个博客框架,我得到:500 Internal Server Error。希望我放一个控制台:

 app.use(function(error, req, res, next) {
    res.status(500).render('500');
      console.log(error);
  });

我可以看到没有描述性的错误:

TypeError: Cannot read property 'replace' of undefined

经过几分钟的研究,我找到了这个答案,所以我尝试了:

 app.use(function(error, req, res, next) {
    res.status(500).render('500');
      console.log(error.stack);
  });

我可以看到错误的根源:(Object.exports.replace)

TypeError: Cannot read property 'replace' of undefined
    at Object.exports.replace (/.../some_folder/node.js-blog-engine/node_modules/swig/lib/filters.js:411:15)
    at eval (eval at <anonymous> (/.../some_folder/node.js-blog-engine/node_modules/swig/lib/swig.js:498:13), <anonymous>:41:63)
    at Object.exports.each (/.../some_folder/node.js-blog-engine/node_modules/swig/lib/utils.js:45:11)
    at eval (eval at <anonymous> (/.../some_folder/node.js-blog-engine/node_modules/swig/lib/swig.js:498:13), <anonymous>:26:10)
    at Object.eval [as tpl] (eval at <anonymous> (/.../some_folder/node.js-blog-engine/node_modules/swig/lib/swig.js:498:13), <anonymous>:85:3)
    at compiled (/.../some_folder/node.js-blog-engine/node_modules/swig/lib/swig.js:619:18)
    at /.../some_folder/node.js-blog-engine/node_modules/swig/lib/swig.js:559:20
    at /.../some_folder/node.js-blog-engine/node_modules/swig/lib/swig.js:690:9
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:380:3)

我知道这不是语法错误,但可以用来查找 javascript/node 错误的位置。

我是nodejs的新手。

我希望这有帮助。

于 2016-07-31T03:14:20.880 回答
0

您可以使用该stacktracey库(注意:我是该库的作者)。

它打印漂亮的调用堆栈以及源代码行,还解析 SyntaxError 输出(在高于 v4 的 Node 中)。

例如,当尝试要求此文件(名为test_files/syntax_error.js)时:

// next line contains a syntax error (not a valid JavaScript)
foo->bar ()

...抛出的错误的漂亮打印调用堆栈将是:

at (syntax error)                  test_files/syntax_error.js:2  foo->bar ()
at it                              test.js:184                   try { require ('./test_files/syntax_error.js') }
at runCallback                     timers.js:781
at tryOnImmediate                  timers.js:743
at processImmediate [as _immediat  timers.js:714

util.inspect...第一行是通过解析 Node.js 中调用的原始输出生成的。

该库还提供对已解析调用堆栈内容的完整 API 访问,因此您可以将输出调整为您想要的任何内容。

于 2017-10-18T22:56:07.173 回答