Last active
September 12, 2018 07:49
-
-
Save soichisumi/dcefdaab4d741176d7c5b2fc239a57a8 to your computer and use it in GitHub Desktop.
nodejs's require caches the module. the cached result is used for second call of require
This file contains hidden or 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
| console.log(`\tfunc1 top\n`); | |
| module.exports = () => { | |
| console.log(`\tfunc1 function\n`); | |
| } |
This file contains hidden or 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
| console.log(`\tfunc2 top\n`); | |
| module.exports = ()=>{ | |
| console.log(`\tfunc2 function\n`); | |
| } |
This file contains hidden or 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
| console.log(`indexjs top\n`); | |
| console.log(`load func1.`) | |
| const func1_1 = require(`./func1`); | |
| const func1_2 = require(`./func1`); | |
| console.log(`func1 loaded.`) | |
| func1_1(); | |
| func1_2(); | |
| console.log(`func1 executed.`) | |
| console.log(`load func2.`) | |
| const func2_1 = require(`./func2`); | |
| const func2_2 = require(`./func2`); | |
| console.log(`func2 loaded.`) | |
| func2_1(); | |
| func2_2(); | |
| console.log(`func2 executed.`) |
This file contains hidden or 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
| title |
This file contains hidden or 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
| my_terminal_home % node ../moduleTest/index.js | |
| indexjs top | |
| load func1. | |
| func1 top | |
| func1 loaded. | |
| func1 function | |
| func1 function | |
| func1 executed. | |
| load func2. | |
| func2 top | |
| func2 loaded. | |
| func2 function | |
| func2 function | |
| func2 executed. |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
func1 and func2 called twice, but, the result shows "func1 top" and "func2 top" showed once.
each module is cached by nodejs runtime.