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.
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()
?
thanks!
looks like a try/catch block does the trick on this e.g.
If the issue is with files that don't exist, what you should do is:
Use a wrapper function:
Usage:
Based on OP answer of course
https://nodejs.org/api/modules.html#modules_file_modules
So do a require in a try catch block and check for
error.code == 'MODULE_NOT_FOUND'