如何检查节点是否存在模块是否存在加载? [英] How to check in node if module exists and if exists to load?
本文介绍了如何检查节点是否存在模块是否存在加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要检查文件/(自定义)模块js是否存在于某些路径下。我尝试像
I need to check if file/(custom)module js exists under some path. I tried like
var m = require('/ home / test_node_project / per');
但是当路径中没有per.js时会抛出错误。
如果文件存在但我不想添加',我想要检查
fs
。js' code>作为后缀,如果可以检查没有。
如何检查模块是否存在,如果存在加载?
var m = require('/home/test_node_project/per');
but it throws error when there is no per.js in path.
I thought to check with
fs
if file exists but I don't want to add '.js'
as suffix if is possible to check without that.
How to check in node if module exists and if exists to load ?
推荐答案
需要一个同步操作,所以你可以将它包装在try / catch中。
Require is a synchronous operation so you can just wrap it in a try/catch.
try {
var m = require('/home/test_node_project/per');
// do stuff
} catch (ex) {
handleErr(ex);
}
这篇关于如何检查节点是否存在模块是否存在加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文