繁体   English   中英

需要从同一文件导出同一目录下的两个模块不起作用

[英]Exporting two module under same directory not working when requiring from same file

我在同一目录下有两个js文件: main/file1.jsmain/file2.js 然后在我的测试文件夹test/files.js下调用它

如果我的file1.js中包含以下内容:

function file1(){
    let result = "a";
    return result;
}
module.exports = file1

然后我的file2.js:

let v = "c" //this is the error that make file2 undefined.  scope issue.
function file2(){
    let result = "b";
    return v;
}
module.exports = file2

然后在我的测试文件中,我需要两个文件。 file1的steps功能可以正常工作,但file2的steps2未定义。 任何想法?

const assert = require('assert'); 
const steps = require('../main/steps');
const steps2 = require('../main/steps2');

describe('steps', function(){
    it('make steps', function(){
        assert.equal(file1(), 'a');
    });
    it('make steps2', function(){
        assert.equal(file2(), 'b');
    });
})

当您需要文件名时,就需要函数名。 您的需求应如下所示:

const steps = require('../main/file1');
const steps2 = require('../main/file2');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM