nephets nephets - 6 months ago 12
Javascript Question

Exit from loading Node module early without stopping process

I have a module which gets

required
, but I temporarily don't want to run the code in the module. I could just comment it out, but then it got me wondering if there is a way to exit/return early from a loading module.

Is there a built in way to stop the execution flow through the module's code, and "return" early?

Answer

There isn't a build-in way, but you can use the following workaround. Wrap the whole module in an IIFE and just use return statement:

(function() {

  // some code

  // stop execution here
  return;

  // some code

})()