Any repercussions to requiring the same file twice in node.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

Tags:

Node.Js