You should use require.resolve() instead of require(). require will load the library if found, but require.resolve() will not, it will return the file name of the module.
See the documentation for require.resolve
try {
console.log(require.resolve("mocha"));
} catch(e) {
console.error("Mocha is not found");
process.exit(e.code);
}
require.resolve() does throw error if module is not found so you have to handle it.