Search code examples
node.jscommonjs

Side-effects of not loading node modules globally


Normally, we require node modules like:

var dep = require('some-dependency');
function runner() {
  dep();
}

If we do like this:

function runner() {
var dep = require('some-dependency');
dep();
}

I know for fact it works, and there's nothing stopping me from doing this, except Eslint's global-require rule.

But is there any side effect in the latter pattern, considering how Node handles it internally?


Solution

  • I believe I got my answer here.

    TL;DR:
    No side effect as such, except for the require operation being blocking, so you may not want to put it deep inside your app.