Can't seem to find any articles on this anywhere. I basically want to catch the "Cannot find module" error from within the program, and optionally ask to install it, but I can't seem to catch any errors even with a try/catch around my require statements. Is this even possible? I haven't seen it done anywhere.
For example:
try {
var express = require('express');
} catch (err){
console.log("Express is not installed.");
//proceed to ask if they would like to install, or quit.
//command to run npm install
}
I suppose this could be done with a seperate .js startup file without any 3rd party requires, and simply uses fs to check for node_modules, and then optionally runs npm install from child process, then runs node app with another child. But it feels like it would be easier to do this from within a single app.js file
requireitself suggests that this module is essential for the app to run.Cannot find moduleis a self-explanatory fault message if you ask me. As a developer it is your responsibility to include all dependencies to your package if you want others to use your app.package.jsonandnpm install. My hunt for this solution is for the sake of making my app as user friendly and automatic as possible.import)