简体   繁体   English

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

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

I have two js file under same directory: main/file1.js and main/file2.js . 我在同一目录下有两个js文件: main/file1.jsmain/file2.js Then I call it under my test folder test/files.js 然后在我的测试文件夹test/files.js下调用它

If I have this in my file1.js: 如果我的file1.js中包含以下内容:

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

Then my file2.js: 然后我的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

Then in my test file I am requiring both files. 然后在我的测试文件中,我需要两个文件。 file1's steps function works fine but the file2's steps2 is undefined. file1的steps功能可以正常工作,但file2的steps2未定义。 Any thought? 任何想法?

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');
    });
})

You're requiring the function name when you should be requiring the file name. 当您需要文件名时,就需要函数名。 Your requires should look something like this: 您的需求应如下所示:

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

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

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