How to use require to execute an IIFE that takes arguments

44 Views Asked by At

I know that when you do require('./file') where the file.js export is an IIFE, that function will immediately run (obviously). I'm just not sure how to deal with this if the IIFE takes arguments.

I'm new to JS go easy on me please.

For something like this:

//iifeFile.js

module.exports =
   (function(arg1, arg2){
      //stuff
   })();

I feel like I read somewhere that you could run it this way

//index.js

const a = require('./iifeFile')(arg1, arg2);

but that throws TypeError: require(...) is not a function

What is the correct syntax to call this?

1

There are 1 best solutions below

5
Unmitigated On

Make the export a regular function:

module.exports = function(arg1, arg2) {
    // ...
    // return something
};