0

I have a bunch of js script files that use require to require the same series of libraries, etc.'

let path = require('path');
let _ = require('underscore');

I want to put all these requirements into a separate library file that I can then reuse amongst the files that need them. I though I could do something like this:

var common = function() {
    this.requireFiles = function() {
        let path = require('path');
        let _ = require('underscore');
        ...
    }
};
export.common = common;

However, when I want to include this library in those files that use all these same files, it does not work. I am trying something like this:

let CommonTools = require('../server_libs/commonFiles').common;
let commonFiles = new CommonTools();
migration.requireFiles();

This give me an error that _ is not a function, when I want to use the underscore methods. Any hints as to where I should look for better understanding on this topic?

thehme
  • 2,698
  • 4
  • 33
  • 39
  • `let _` is only local to the scope inside that function. It's not in your module scope. To put it in your module scope, you will have to assign it into a module scope variable on the calling end of things (not inside your `requireFiles()` function). That's just what you have to do with modules unless your `requireFiles()` function puts it into the global namespace (not recommended). – jfriend00 May 10 '18 at 21:06
  • @jfriend00 can you give me a link to good explanation of how this might work? – thehme May 11 '18 at 14:22

1 Answers1

0

I personally do not recommend making a common module. The node.js module mentality is to just require() in what a module needs. Yes, it seems like a little extra/redundant typing in each module, but it makes each module self describing and does not build any unnecessary dependencies between modules leading to the simplest module sharing or reuse options. Modules are cached by the require() sub-system so it doesn't really cost you at runtime to just require() in each module as you need them. This is pretty much the node.js way.

That said, if you really want to make a common module, you can do it like this:

common.js

module.exports = {
    _: require('underscore');
    path: require('path');
}

otherModule.js

const {_, path} = require('common.js');

// can now call underscore methods _.each()
// can now call path methods path.join()

This uses destructing assignment to get properties from the common.js exports and assign them to a module-scoped variable and to do it for multiple properties in one statement. It still requires you to list each property you want defined in this module (which helps self describe what you're doing).

This also assume you're using require() and module.exports. If you're using the newer import and export keywords, then you can modify the syntax accordingly, but still use the same concept.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • I see, thanks for the advice, this helped with what I needed, and it's good for me to learn about this perspective. Thanks for the link, I want to understand this better. – thehme May 11 '18 at 19:21