首页 > 解决方案 > 引用的变量未定义

问题描述

我在一个模块(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

抱歉,这可能是我想念的简单但看不到的东西。

标签: javascriptnode.js

解决方案


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


推荐阅读