javascript - Why is it possible to access 'module.exports' as just 'exports' but not so with 'module.id'? -


this code in file named foo.js.

console.log('module.exports:', module.exports) console.log('module.id:', module.id) console.log('exports:', exports) console.log('id:', id) 

this output get.

$ node foo.js module.exports: {} module.id: . exports: {} /home/lone/foo.js:4 console.log('id:', id)                    ^  referenceerror: id not defined     @ object.<anonymous> (/home/lone/foo.js:4:20)     @ module._compile (module.js:573:30)     @ object.module._extensions..js (module.js:584:10)     @ module.load (module.js:507:32)     @ trymoduleload (module.js:470:12)     @ function.module._load (module.js:462:3)     @ function.module.runmain (module.js:609:10)     @ startup (bootstrap_node.js:158:16)     @ bootstrap_node.js:598:3 

here unable understand. both exports , id attributes of module object. able access exports without module. qualifier unable id attribute.

why so? concepts @ play here makes possible access module.exports exports not module.id?

this how nodejs behaves. every code write wrapped self-calling function specific arguments.

(function(exports, require, module, __filename, __dirname) {     // code })() 

that why module , require available directly.

why so? concepts @ play here makes possible access module.exports exports not module.id?

it's not can access properties of module, exports has been provided explicitly ease of access.

important note: exports , module.exports have same reference, means change either 1 reflect on other.

nodejs docs: https://nodejs.org/api/modules.html#modules_the_module_wrapper

more reference: https://www.youtube.com/watch?v=9wufqlwfuwm&list=plkt6ojq4t2f-sl50i51a64jbocknffjy9


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -