Categories
JavaScript Answers

How to check in Node if module exists and if exists to load?

Spread the love

to check in Node if module exists and if exists to load, we add a try-catch block.

For instance, we write

try {
  const m = require("/home/test_node_project/per");
} catch (ex) {
  handleErr(ex);
}

to call require to require the /home/test_node_project/per module.

If the module can’t be included, then the catch block runs.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.