In NodeJs, each file is an independent module, and the variables defined inside are in a local scope and cannot be accessed from outside. So, you referenced lib in a.js, probably like this a.js 中引用了 lib,可能是这样
lib here is a local variable. Once this file is out, it cannot be accessed from other places (unless exported). So if you still need to reference lib in b.js, you have to
rrreee
The lib here and the previous lib in a.js actually point to the same object (both are exported from lib.js), but they (refer to variables ) are in different (independent) scopes. 🎜
🎜If you have written PHP before, you may have difficulty understanding this. PHP's include is equivalent to inserting the included script at the current location, while require in node just introduces the object exported by the corresponding script. If you look at the code of requirejs or seajs, you will understand what node's require is doing. 🎜
In NodeJs, each file is an independent module, and the variables defined inside are in a local scope and cannot be accessed from outside. So, you referenced
lib
ina.js
, probably like thisa.js
中引用了lib
,可能是这样这里的
lib
是一个局域变量,出了这个文件,其它地方就访问不到了(除非 export)。所以在b.js
中如果你还需要引用lib
的话,就得这里的
lib
和之前 a.js 中的lib
rrreeelib
here is a local variable. Once this file is out, it cannot be accessed from other places (unless exported). So if you still need to referencelib
inb.js
, you have torrreee
Thelib
here and the previouslib
in a.js actually point to the same object (both are exported from lib.js), but they (refer to variables ) are in different (independent) scopes. 🎜 🎜If you have written PHP before, you may have difficulty understanding this. PHP's include is equivalent to inserting the included script at the current location, while require in node just introduces the object exported by the corresponding script. If you look at the code of requirejs or seajs, you will understand what node's require is doing. 🎜Each module is independent. To modify mongoose in use, you must require to get the mongoose object, otherwise how can you modify it