使用另一个文件中导出函数内部定义的函数

时间:2020-03-23 21:08:39

标签: javascript node.js

我在helperFunctions.js文件中有一些类似的代码:

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

如何从一个单独的文件(例如main.js)中分别调用functionAfunctionB

我尝试过:

import { helperFunctions } from './helperFunctions';

//...some code

helperFunctions.functionA('hello');

// OR

functionA('hello');

具体错误是:

TypeError: _helperFunctions.helperFunctions.functionA is not a function

当尝试第二种解决方案时,它是:

ReferenceError: functionA is not defined

我试图避免从字面上导入我正在使用的每个函数(通过导出我正在使用的每个函数)。我想为自己需要的功能做类似helperFunctions.function的事情。

1 个答案:

答案 0 :(得分:1)

真的需要一个功能吗?您可以导出对象:

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

exports helperFunctions;