简体   繁体   English

如何在 nodejs es 模块上显示有用的错误回溯?

[英]How to show useful error backtrace on nodejs es module?

I try to use es module with nodejs, but seems that it's error backtrace dosen't show where the error was thrown?我尝试将 es 模块与 nodejs 一起使用,但似乎它的错误回溯并没有显示错误发生的位置?

// copy and rename to `a.cjs` and `a.mjs`
console.log("hi")
throw "err"
node --trace-uncaught a.cjs
node --trace-uncaught a.mjs

CommonJS CommonJS

[kkocdko@fedora kblog]$ node --trace-uncaught a.cjs
hi

/home/kkocdko/misc/code/kblog/a.cjs:2
throw "err"
^
err
Thrown at:
    at /home/kkocdko/misc/code/kblog/a.cjs:2:1
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Module._load (node:internal/modules/cjs/loader:822:12)
    at executeUserEntryPoint (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

ES Module ES模块

[kkocdko@fedora kblog]$ node --trace-uncaught a.mjs
hi

node:internal/process/esm_loader:94
    internalBinding('errors').triggerUncaughtException(
                              ^
err
Thrown at:
    at loadESM (node:internal/process/esm_loader:94:31)

The CommonJS version reported the position of error, but ES Module does not. CommonJS版本报错position,而ES Module没有。 This cause debuging to be difficult.这导致调试很困难。 How to show useful error backtrace on nodejs es module?如何在 nodejs es 模块上显示有用的错误回溯?

Throwing an Error populates the stack, throwing a String does not.抛出一个Error会填充堆栈,抛出一个String不会。

throw new Error('err')
$ node a.mjs
file:///so73742023/a.mjs:7
throw new Error('err')
      ^

Error: err
    at file:///so73742023/a.mjs:7:7
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:528:24)
    at async loadESM (node:internal/process/esm_loader:91:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

Don't throw strings.不要扔字符串。

Have you tried console.error OR Error.captureStackTrace ?您是否尝试过console.errorError.captureStackTrace If these don't work for you then you could try using the node-stack-trace module, a power full module to track call stacks.如果这些对您不起作用,那么您可以尝试使用node-stack-trace模块,这是一个强大的模块来跟踪调用堆栈。

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

相关问题 Azure 应用服务 - Nodejs ES 模块错误 - Azure App Service - Nodejs ES Module Error 如何在NodeJS中获得自定义错误类的正确回溯? - How do I get a correct backtrace for a custom error class in NodeJS? ES6功能:模板字符串如何有用? - ES6 Feature: How are Template Strings useful? nodejs exec命令失败,没有有用的错误消息 - nodejs exec command failing with no useful error message TypeScript & NodeJS:ES 模块中未定义导出 scope - TypeScript & NodeJS: Exports is not defined in ES module scope 省略文件扩展名,ES6 模块 NodeJS - Omit the file extension, ES6 module NodeJS NodeJS Typescrip.:出现错误:必须使用导入在 sls 离线命令上加载 ES 模块 - NodeJS Typescrip.: Getting error: Must use import to load ES Module on sls offline command 如何导出使用 NodeJS 中的 ES6 模块动态导入的 class 的实例? - How to export the instance of the class which is imported dynamically with ES6 module in NodeJS? 关于Nodejs模块的错误 - An Error about Nodejs module 如何通过es6导入导入nodejs模块“module”以使用“createRequire”创建“require”函数以使用节点的“require”? - How to import nodejs module "module" via es6 import to create "require" function with "createRequire" in order to use node's "require"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM