You can have multiple named exports per module but only one default export.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With