Advertisement
robertbira

Italian Translation Report: Node.js [Part 32 - 1238 words]

Sep 5th, 2018
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. In the Node.js module system, each file is treated as a separate module.
  2. For example, consider a file named
  3. On the first line, loads the module that is in the same directory as
  4. Here are the contents of
  5. The module has exported the functions and
  6. Functions and objects are added to the root of a module by specifying additional properties on the special object.
  7. Variables local to the module will be private, because the module is wrapped in a function by Node.js (see module wrapper).
  8. In this example, the variable is private to
  9. The property can be assigned a new value (such as a function or object).
  10. Below, makes use of the module, which exports a Square class:
  11. The module is defined in
  12. assigning to will not modify module, must use
  13. The module system is implemented in the module.
  14. Accessing the main module
  15. When a file is run directly from Node.js, is set to its
  16. That means that it is possible to determine whether a file has been run directly by testing
  17. For a file, this will be if run via, but if run by
  18. Because provides a property (normally equivalent to), the entry point of the current application can be obtained by checking
  19. Addenda: Package Manager Tips
  20. The semantics of Node.js's function were designed to be general enough to support a number of reasonable directory structures.
  21. Package manager programs such as and will hopefully find it possible to build native packages from Node.js modules without modification.
  22. Below we give a suggested directory structure that could work:
  23. Let's say that we wanted to have the folder at hold the contents of a specific version of a package
  24. Packages can depend on one another.
  25. In order to install package, it may be necessary to install a specific version of package
  26. The package may itself have dependencies, and in some cases, these may even collide or form cyclic dependencies.
  27. Since Node.js looks up the of any modules it loads (that is, resolves symlinks), and then looks for their dependencies in the
  28. folders as described here, this situation is very simple to resolve with the following architecture:
  29. Contents of the package, version 1.2.3.
  30. Contents of the package that depends on.
  31. Symbolic link to
  32. Symbolic links to the packages that depends on.
  33. Thus, even if a cycle is encountered, or if there are dependency conflicts, every module will be able to get a version of its dependency that it can use.
  34. When the code in the package does, it will get the version that is symlinked into
  35. Furthermore, to make the module lookup process even more optimal, rather than putting packages directly in, we could put them in
  36. Then Node.js will not bother looking for missing dependencies in or
  37. In order to make modules available to the Node.js REPL, it might be useful to also add the folder to the environment variable.
  38. Since the module lookups using folders are all relative, and based on the real path of the files making the calls to the packages themselves can be anywhere.
  39. All Together...
  40. To get the exact filename that will be loaded when is called, use the function.
  41. Putting together all of the above, here is the high-level algorithm in pseudocode of what does:
  42. require(X) from module at path Y
  43. If X is a core module,
  44. return the core module
  45. If X begins with
  46. set Y to be the filesystem root
  47. If X begins with or
  48. "not found"
  49. If X is a file, load X as JavaScript text.
  50. parse to a JavaScript Object.
  51. load as binary addon.
  52. If is a file, Parse and look for "main" field.
  53. let
  54. for each in:
  55. while
  56. if
  57. return
  58. Caching
  59. Modules are cached after the first time they are loaded.
  60. This means (among other things) that every call to will get exactly the same object returned, if it would resolve to the same file.
  61. Multiple calls to may not cause the module code to be executed multiple times. This is an important feature. With it, "partially done" objects can be returned, thus allowing transitive dependencies to be loaded even when they would cause cycles.
  62. To have a module execute code multiple times, export a function, and call that function.
  63. Module Caching Caveats
  64. Modules are cached based on their resolved filename.
  65. Since modules may resolve to a different filename based on the location of the calling module (loading from folders), it is not a guarantee that will always return the exact same object, if it would resolve to different files.
  66. Additionally, on case-insensitive file systems or operating systems, different resolved filenames can point to the same file, but the cache will still treat them as different modules and will reload the file multiple times.
  67. For example, and return two different objects, irrespective of whether or not and are the same file.
  68. Core Modules
  69. Node.js has several modules compiled into the binary. These modules are described in greater detail elsewhere in this documentation.
  70. The core modules are defined within Node.js's source and are located in the folder.
  71. Core modules are always preferentially loaded if their identifier is passed to
  72. For instance, will always return the built in HTTP module, even if there is a file by that name.
  73. Cycles
  74. When there are circular calls, a module might not have finished executing when it is returned.
  75. Consider this situation:
  76. When loads, then in turn loads
  77. At that point, tries to load
  78. In order to prevent an infinite loop, an unfinished copy of the exports object is returned to the module.
  79. then finishes loading, and its object is provided to the module.
  80. By the time has loaded both modules, they're both finished.
  81. The output of this program would thus be:
  82. Careful planning is required to allow cyclic module dependencies to work correctly within an application.
  83. File Modules
  84. If the exact filename is not found, then Node.js will attempt to load the required filename with the added extensions: and finally
  85. files are interpreted as JavaScript text files, and files are parsed as JSON text files.
  86. files are interpreted as compiled addon modules loaded with
  87. A required module prefixed with is an absolute path to the file.
  88. For example, will load the file at
  89. A required module prefixed with is relative to the file calling
  90. That is, must be in the same directory as for to find it.
  91. Without a leading or to indicate a file, the module must either be a core module or is loaded from a folder.
  92. If the given path does not exist, will throw an with its property set to
  93. Folders as Modules
  94. It is convenient to organize programs and libraries into self-contained directories, and then provide a single entry point to that library.
  95. There are three ways in which a folder may be passed to as an argument.
  96. The first is to create a file in the root of the folder, which specifies a module.
  97. An example file might look like this:
  98. If this was in a folder at, then would attempt to load
  99. This is the extent of Node.js's awareness of files.
  100. If the file specified by the entry of is missing and can not be resolved, Node.js will report the entire module as missing with the default error:
  101. If there is no file present in the directory, then Node.js will attempt to load an or file out of that directory.
  102. For example, if there was no file in the above example, then would attempt to load:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement