2014-09-24 54 views
0

我正在研究一些需要shellj的cordova钩子。现在,如果用户没有安装shelljs,他们得到此错误消息处理节点中缺少的模块错误

module.js:340 
    throw err; 
     ^
Error: Cannot find module 'shelljs' 
    at Function.Module._resolveFilename (module.js:338:15) 
    at Function.Module._load (module.js:280:25) 
    at Module.require (module.js:364:17) 
    at require (module.js:380:17) 
    .... 

所以这个消息很丑陋,我想提供更好的信息给用户。无论如何阻止这条消息,然后通过console.log允许自定义消息?

回答

1

您可以在一个try-catch包起来:

var shelljs; 
try { 
    shelljs = require('shelljs'); 
} catch (ex) { 
    console.log('ShellJS is required'); 
    process.exit(1); 
} 
+0

真棒,谢谢。任何方式摆脱这一点? '钩子失败,出现错误代码1:' – mhartington 2014-09-24 22:34:41

+0

你可以发送完整的输出到某处(尤其是之前发生的错误信息)吗?这一点并没有多大的意义。 – mscdex 2014-09-24 22:58:35

+0

那么输出就是代码退出的地方,或者什么文件导致退出 – mhartington 2014-09-24 23:46:28