I have a general and high-level understanding of the functionality and nature of Node.js' require()and module.exports. However, there are some behaviors that do not make sense to me.
Let's say I have two very simple single-line files a.jsand b.js.
In a.js:
require('./b.js');
and in b.js:
console.log("testing");
and if I run node a.jsin the terminal, here is what is written:
$ node a.js
testing.
which means that only requiring a file / module, the requested content of the file is exposed to the file that issues the request (, right?)
Now I am changing a.jsas follows:
require('./b.js');
testFunc(1, 2);
and b.js:
var testFunc = function(a, b) {
var result = a + b;
return result;
}
and run again node a.jsin terminal:
$ node a.js
/demo/a.js:3
testFunc(1, 2);
^
ReferenceError: testFunc is not defined
......
, ? -, , b.js, a.js b.js. , b.js, , ReferenceError: testFunc is not defined. ?
, require() script, ? module.exports?