简体   繁体   中英

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 . Then I call it under my test folder test/files.js

If I have this in my file1.js:

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

Then my 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. 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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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