引用变量未定义

时间:2020-10-09 15:00:24

标签: javascript node.js

我想在一个模块(setup.js)中使用一个函数,但还要在另一个模块(config.js)中引用完全相同的变量。

不幸的是,当我尝试从另一个模块引用所需的导出变量时,我得到“未定义”。

setup.js

    let randomString = function() {
        return Math.random().toString(36).slice(2);;
    };
    
    console.log(randomString());
    //SxIARFSSvw

    module.exports = randomString;

导入到另一个文件

config.js

    const setup = require('./setup');
    console.log(setup.randomString);
    //undefined, looking for SxIARFSSvw

很抱歉,这很简单,我想念但看不到。

1 个答案:

答案 0 :(得分:0)

module.exports = randomString;更改为module.exports = {randomString: randomString};