处理node.js中require()模块抛出的错误

编程入门 行业动态 更新时间:2024-10-24 03:25:06
本文介绍了处理node.js中require()模块抛出的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在尝试要求()不存在的模块时,我的代码中有一点阻碍。 代码循环遍历一个目录,并在每个文件夹上执行一个 var appname = require('path')。这适用于适当配置的模块,但抛出:错误:当循环命中非模块时,找不到模块。

having a bit of a snag in my code when trying to require() modules that don't exists. The code loops through a directory and does a var appname = require('path') on each folder. This works for appropriately configured modules but throws: Error: Cannot find module when the loop hits a non-module.

我想要能够妥善地处理这个错误,而不是让它停止我的整个过程。所以简而言之,如何捕获由 require()抛出的错误?

I want to be able to handle this error gracefully, instead of letting it stop my entire process. So in short, how does one catch an error thrown by require()?

谢谢!

推荐答案

看起来像一个try / catch块这样做的技巧例如

looks like a try/catch block does the trick on this e.g.

try { // a path we KNOW is totally bogus and not a module require('./apps/npm-debug.log/app.js') } catch (e) { console.log('oh no big error') console.log(e) }

更多推荐

处理node.js中require()模块抛出的错误

本文发布于:2023-11-15 04:05:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1590989.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:抛出   模块   错误   node   js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!