Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS "this" empty object

Why is the "this" an empty object in NodeJS? Where does the "module.exports" belongs to?

I thought that "module.exports..." could be written as "this.module.exports..." but it won't work.

Thank you

like image 481
eSinxoll Avatar asked Dec 04 '22 01:12

eSinxoll


1 Answers

If you are in an actual module:

  • module is a reference to the current module

  • exports is a reference to the exported data. It gets cached away by NodeJS and delivered to other modules that require it

  • this is an alternate reference to the exports object

  • module.exports is an alternate reference to the exports object

  • this.module is undefined

this is empty because as noted above it is a reference to the same object as exports, which is to be populated by the developer.

If this.module was a reference to module, then since this is a reference to exports, it would export the module itself along with the other exported items. I doubt this would be desired.


If you are in the REPL, then this.module is defined.

like image 103
I Hate Lazy Avatar answered Dec 22 '22 18:12

I Hate Lazy