Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to access other module export functions within the same file?

Tags:

People also ask

Can you have multiple exports in one file?

You can have multiple named exports per module but only one default export.

Can you export with module exports?

When you export a module, you can import it into other parts of your applications and consume it. Node. js supports CommonJS Modules and ECMAScript Modules.

How do I export multiple functions?

To export multiple functions in JavaScript, use the export statement and export the functions as an object. Alternatively, you can use the export statement in front of the function definitions. This exports the function in question automatically and you do not need to use the export statement separately.

CAN module exports be a function?

Note: By default, exports is an object, but it can also be a function, number, string, etc.


I have two functions in the same file, both accessed externally. One of the functions is called by the second.

module.exports.functionOne = function(param) {
    console.log('hello'+param);
};

module.exports.functionTwo = function() {
    var name = 'Foo';
    functionOne(name);
};

When this gets executed, the call to functionOne is flagged as not defined.

What's the right way to reference it?

One pattern I've found to work is by referencing the file itself.

var me = require('./thisfile.js');
me.functionOne(name);

... but it feels like there has to be a better way.