Node.js · Junior · Question 17 of 75
What does require() do if the same module is required twice?
- 1Throws an error about a duplicate import
- ✓Correct answer: Returns the cached export; the module code runs only once
- 3Runs the module code again and returns a new object
Explanation
Modules are cached after the first load (in require.cache). That's why a module can hold shared state, such as a database connection, for the whole app. ES modules are cached too.