Will I run into any problems if I require the same file twice?
require('myclass.js');
require('myclass.js');
Absolutely none. Modules are cached the first time they are loaded so the second call is just a no-op.
I discovered a caveat, resulting from the fact that requiring twice is a no-op: requiring a file, mutating the object returned by that file, and requiring the file again will not undo the mutations.
Example:
let path;
path = require('path');
console.log(path.asdf);
path.asdf = 'foo';
console.log(path.asdf);
path = require('path');
console.log(path.asdf);
This produces the following output:
undefined
foo
foo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With