How to import a module in es6 that itself needs to invoke/initialize its function/class before being imported

Ali picture Ali · Aug 16, 2016 · Viewed 10.7k times · Source

I was wondering what is the best practice to import a module's function/class in another module that the module itself needs to invoke/initialize its own function/class before being imported into another module? I don't know if I could ask my question clearly enough! So let's put it in an example.

This is my module:

// myModule.js
class MyModule {
  constructor() {
    // do sth
  }
}

let myModule = new MyModule();

And this is how I like to import it in another module:

import MyModule from './myModule';

This actually works fine! But as you can see, in the myModule.js file I didn't export default my MyModule class because that's not the only thing that is happening in the myModule.js file! I'm also initializing the class after defining it... (I know that even if I have set my class as export default the initializing would still work fine while the module is imported somewhere else...)

So, without setting anything as exported inside of our module, or with setting the class as the export default, everything works fine when the module has been imported somewhere else... So far so good! But I'm looking for a best practice if there's one!

So here are my questions regarding such cases:

  1. Is it ok to import a module that doesn't have anything for export?
  2. Shall we set the class as the export default, although we are doing some more works outside of the class in the module (The initializing job that is happening after defining the class)?
  3. Or maybe is it good to do the initializing job in another function and then export both, the class and the function, and then invoke the function to do the initializing job in the imported module?

Thanks a lot everyone! I really appreciate any helps regarding this :)

Answer

Oliver picture Oliver · Aug 16, 2016

How about offering to import the class or the instance of it? Like:

// export class itself
export class MyModule {
  constructor() {
    // do sth
  }
}

// export instance of MyModule directly
export default new MyModule();

// export a factory function if you need more work to be done
// before the instance is created
export function myModuleFactory(...args) { // define e.g. arguments to be passed to constructor
  // ... do stuff
  const myModule = new MyModule(...args);
  // ... do more stuff
  return myModule;
}

So you could do:

// import instance
import instance from './myModule';
// or class
import { MyModule } from './myModule';
// or import factory
import { myModuleFactory } from './myModule';

What to do, depeneds on what you want to accomplish with your module. If you want your app use one shared instance of a MyModule class object, you’d export and import the instance like shown above. If you want to create multiple instances in different contexts, you’d export the class itself or a factory function to return a new instance.

To keep it even more clean, you’d keep the class in yet another separate file and import it into the module providing the factory/instantiation.

Update

To answer your first question: You can import modules that don’t have any export defined. The module will be loaded and its logic executed. The thing is that as long as it won’t change global variables (like window in web development) it won’t have any effect as everything inside the module happens within an isolated scope. As you may have guessed, having modules change global vars is far from best practice.