我想在一个模块(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
很抱歉,这很简单,我想念但看不到。
答案 0 :(得分:0)
将module.exports = randomString;
更改为module.exports = {randomString: randomString};