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

前端 未结 3 972
广开言路
广开言路 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:35

    Change a.js to export the variable:

    exports.test = "Hello World";
    

    and assign the return value of require('./a.js') to a variable:

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

    Another pattern you will often see and probably use is to assign something (an object, function) to the module.exports object in a.js, like so:

    module.exports = { big: "string" };
    

提交回复
热议问题