What happens to modules required in exported modules Node?

user137717

I've been trying to modularize my server and web apps. i've read about exports and module.exports in Node here and here.

http://www.sitepoint.com/understanding-module-exports-exports-node-js/

http://liangzan.net/blog/blog/2012/06/04/how-to-use-exports-in-nodejs/

I get how it makes the functions available to other files / scripts, but what happens to the modules required within the exported file? If I require websockets in some file, export it and require it within another file, does the other file inherit the websockets from the exported file? Is it similar to a header file in C and just pastes that module into your file?

mscdex

When you require() a module in node, it gets executed inside a closure and the value exported by the module is cached. So any additional require()s for the same module (located at the same absolute path) will always get the same object/value/whatever.

So in your websockets example, the require('websockets') that you do in your module is not automagically available to anyone requiring your module. Everything in a module is done within a separate, local scope (you can read/write the global scope accessible by all modules, but you really should not do that). This is why you need to explicitly export values for them to be seen by outsiders.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

No Exported Member / Node Modules

From Dev

What part of node modules is required in production and development?

From Dev

Node js callbacks in exported modules

From Dev

Node js callbacks in exported modules

From Dev

What happens when we reload Elixir modules?

From Dev

What happens when we reload Elixir modules?

From Dev

required node/express modules not updating with chokidar?

From Dev

@type for "exported modules" from node.js and good documentation descriptions?

From Dev

@type for "exported modules" from node.js and good documentation descriptions?

From Dev

Inheritance TypeScript with exported class and modules

From Dev

What is the purpose of .bin folder in node_modules?

From Dev

What is node_modules directory in ember

From Dev

"Required module not found" for module that exists in node_modules

From Dev

Is node_modules directory required on a remote server for React project?

From Dev

Node.js express: access application config from required modules

From Dev

Lua: what scope do variables in "required" modules go to?

From Dev

How does import work with node modules in referencing a module's exported name?

From Dev

'latLng' is not exported by 'node_modules/leaflet/dist/leaflet-src.js' Rollup build fails on leaflet

From Dev

Msal.js: node_modules/msal/lib-commonjs/index has no exported member 'User'

From Dev

Compiling dynamically required modules with Browserify

From Dev

Compiling required external modules with cython

From Dev

ios - Missing required modules: 'Crypto'

From Dev

Node.js - What's the scope of the require()d modules?

From Dev

What is the relationship between the node_modules directory and package.json?

From Dev

What is the better practice for sharing variables across Node.js modules

From Dev

What is the difference between request and http modules in node.js?

From Dev

What is the better practice for sharing variables across Node.js modules

From Dev

What testing framework should I use to test node js modules?

From Dev

What is the problem with node_modules (or npm) PPA on Ubuntu

Related Related

  1. 1

    No Exported Member / Node Modules

  2. 2

    What part of node modules is required in production and development?

  3. 3

    Node js callbacks in exported modules

  4. 4

    Node js callbacks in exported modules

  5. 5

    What happens when we reload Elixir modules?

  6. 6

    What happens when we reload Elixir modules?

  7. 7

    required node/express modules not updating with chokidar?

  8. 8

    @type for "exported modules" from node.js and good documentation descriptions?

  9. 9

    @type for "exported modules" from node.js and good documentation descriptions?

  10. 10

    Inheritance TypeScript with exported class and modules

  11. 11

    What is the purpose of .bin folder in node_modules?

  12. 12

    What is node_modules directory in ember

  13. 13

    "Required module not found" for module that exists in node_modules

  14. 14

    Is node_modules directory required on a remote server for React project?

  15. 15

    Node.js express: access application config from required modules

  16. 16

    Lua: what scope do variables in "required" modules go to?

  17. 17

    How does import work with node modules in referencing a module's exported name?

  18. 18

    'latLng' is not exported by 'node_modules/leaflet/dist/leaflet-src.js' Rollup build fails on leaflet

  19. 19

    Msal.js: node_modules/msal/lib-commonjs/index has no exported member 'User'

  20. 20

    Compiling dynamically required modules with Browserify

  21. 21

    Compiling required external modules with cython

  22. 22

    ios - Missing required modules: 'Crypto'

  23. 23

    Node.js - What's the scope of the require()d modules?

  24. 24

    What is the relationship between the node_modules directory and package.json?

  25. 25

    What is the better practice for sharing variables across Node.js modules

  26. 26

    What is the difference between request and http modules in node.js?

  27. 27

    What is the better practice for sharing variables across Node.js modules

  28. 28

    What testing framework should I use to test node js modules?

  29. 29

    What is the problem with node_modules (or npm) PPA on Ubuntu

HotTag

Archive