Skip to content

Instantly share code, notes, and snippets.

@mtorre4580
Created April 9, 2023 17:31
Show Gist options
  • Save mtorre4580/8e1666dd1895681235d04986b9ba878f to your computer and use it in GitHub Desktop.
Save mtorre4580/8e1666dd1895681235d04986b9ba878f to your computer and use it in GitHub Desktop.
Example check existence of a module in Node.js
/**
* What pass when require a module?
*
* Steps
*
* 1. Resolving
* 2. Loading
* 3. Wrapping
* 4. Evaluating
* 5. Caching
*/
// Example check existence of a module
try {
require.resolve("fake_module");
} catch (err) {
if (err.code === "MODULE_NOT_FOUND") {
console.log("The current module not exists");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment