NodeJs require('./file.js') issues

前端 未结 3 971
广开言路
广开言路 2021-02-02 05:16

I am having issues including files to execute in my NodeJs project.

I have two files in the same directory:

a.js

var test = \"He         


        
3条回答
  •  南笙
    南笙 (楼主)
    2021-02-02 05:55

    You are misunderstanding what should be happening. The variables defined in your module are not shared. NodeJS scopes them.

    You have to return it with module.exports.

    a.js

    module.exports = "Hello World";
    

    b.js

    var test = require('./a.js');
    console.log(test);
    

提交回复
热议问题