Exit from loading Node module early without stopping process

morsecoder

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?

jfriend00

Actually, there is a built-in way. Every node module is loaded inside a module function wrapper and the module is executing as the body of that function. So you can just use a plain return anywhere in the module to stop the rest of the code from executing.

Node modules get executed inside a function wrapper like this:

(function (exports, require, module, __filename, __dirname, process, global) {  
    // module code is here

    // a return statement skips executing any more code in your module after that statement
    return;
 });

As such, you can just use a return statement anywhere in the module and it will return from the module wrapper function, skipping the rest of the code in the module.


It is probably cleaner code to just use an if statement or in your module to make the code a little more self describing and not leaving somebody wondering why there's an extraneous return statement in the middle of a module:

// this code temporarily removed from being executed
if (someBoolean) {
    // your module code here
}

Or just comment out a large section of code if you intend to remove it for awhile.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Start shell script using php and exit it without stopping the process

From Dev

Exit from REPL without killing background process

From Dev

Exit from REPL without killing background process

From Dev

Can I detect early exit from a long-running, backgrounded process?

From Dev

Can I detect early exit from a long-running, backgrounded process?

From Dev

Loading a specific file from a module in node

From Dev

My module is stopping my Wowza application from loading because of required dependencies

From Dev

My module is stopping my Wowza application from loading because of required dependencies

From Dev

IPython Notebook - early exit from cell

From Java

Early stopping with multiple conditions

From Dev

Stop fglrx from loading on boot/unload fglrx module without uninstalling it

From Dev

Loading a node_module from above the current directory

From Dev

Call a node module from another module without using require() everywhere

From Dev

Call a node module from another module without using require() everywhere

From Dev

want to exit from the main process without wait to process2 return

From Java

Correct way to detach from a container without stopping it

From Dev

Throw sqlite insert error in node application without stopping node server

From Dev

Python: Process in one Thread stopping a Process in another Thread from finishing

From Dev

Exit script from a backgrounded process?

From Dev

Linux: is there a way to use ptrace without stopping/pausing the process (SIGSTOP)?

From Dev

Running SQL Stored Procedure process without stopping on errors

From Dev

stopping/killing a process using command prompt without knowing the PID

From Dev

Early exit a `CNContactStoreEnumerateContactsHandler` Enumeration

From Dev

is it possible to exit a constructor early?

From Dev

Exit recursion early with a value?

From Dev

'exit'-Event in worker process when killed from master within a node.js cluster

From Dev

node js process on exit not work using forever

From Dev

Node.js detect a child process exit

From Dev

Stopping a WordPress plugin from loading a css file in the header

Related Related

  1. 1

    Start shell script using php and exit it without stopping the process

  2. 2

    Exit from REPL without killing background process

  3. 3

    Exit from REPL without killing background process

  4. 4

    Can I detect early exit from a long-running, backgrounded process?

  5. 5

    Can I detect early exit from a long-running, backgrounded process?

  6. 6

    Loading a specific file from a module in node

  7. 7

    My module is stopping my Wowza application from loading because of required dependencies

  8. 8

    My module is stopping my Wowza application from loading because of required dependencies

  9. 9

    IPython Notebook - early exit from cell

  10. 10

    Early stopping with multiple conditions

  11. 11

    Stop fglrx from loading on boot/unload fglrx module without uninstalling it

  12. 12

    Loading a node_module from above the current directory

  13. 13

    Call a node module from another module without using require() everywhere

  14. 14

    Call a node module from another module without using require() everywhere

  15. 15

    want to exit from the main process without wait to process2 return

  16. 16

    Correct way to detach from a container without stopping it

  17. 17

    Throw sqlite insert error in node application without stopping node server

  18. 18

    Python: Process in one Thread stopping a Process in another Thread from finishing

  19. 19

    Exit script from a backgrounded process?

  20. 20

    Linux: is there a way to use ptrace without stopping/pausing the process (SIGSTOP)?

  21. 21

    Running SQL Stored Procedure process without stopping on errors

  22. 22

    stopping/killing a process using command prompt without knowing the PID

  23. 23

    Early exit a `CNContactStoreEnumerateContactsHandler` Enumeration

  24. 24

    is it possible to exit a constructor early?

  25. 25

    Exit recursion early with a value?

  26. 26

    'exit'-Event in worker process when killed from master within a node.js cluster

  27. 27

    node js process on exit not work using forever

  28. 28

    Node.js detect a child process exit

  29. 29

    Stopping a WordPress plugin from loading a css file in the header

HotTag

Archive