Dan
Dan

Reputation: 381

Using a function defined inside an exports function, from another file

I have some code like this in a file helperFunctions.js:

exports helperFunctions = () => {
    const functionA = async(args) => {
        console.log(args);
    };
    const functionB = async(args) => {
        functionA(myArg);
    };
}

How can I call functionA and functionB from a separate file altogether, say, main.js?

I've tried:

import { helperFunctions } from './helperFunctions';

//...some code

helperFunctions.functionA('hello');

// OR

functionA('hello');

The specific error is:

TypeError: _helperFunctions.helperFunctions.functionA is not a function

and when trying the second solution, it's:

ReferenceError: functionA is not defined

I'm trying to avoid importing literally every single function I'm using (by way of exporting every single function I'm using). I'd like to do something like helperFunctions.function for the functions I need.

Upvotes: 0

Views: 28

Answers (1)

Alan Vinícius
Alan Vinícius

Reputation: 166

It really need to be a function? You could export an Object:

// helperFunctions.js
let helperFunctions = {
    functionA: async (args) => {
        console.log(args);
    },
    functionB: async (args) => {
        functionA(myArg);
    }
}

exports helperFunctions;

Upvotes: 1

Related Questions