Created
April 9, 2023 17:31
-
-
Save mtorre4580/8e1666dd1895681235d04986b9ba878f to your computer and use it in GitHub Desktop.
Example check existence of a module in Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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