Advertisement
robertbira

Italian Translation Report: Node.js [Part 33 - 1303 words]

Sep 8th, 2018
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.55 KB | None | 0 0
  1. Loading from Folders
  2. If the module identifier passed to is not a core module, and does not begin with or then Node.js starts at the parent directory of the current module, and adds, and attempts to load the module from that location.
  3. Node will not append to a path already ending in
  4. If it is not found there, then it moves to the parent directory, and so on, until the root of the file system is reached.
  5. For example, if the file at called, then Node.js would look in the following locations, in this order:
  6. This allows programs to localize their dependencies, so that they do not clash.
  7. It is possible to require specific files or sub modules distributed with a module by including a path suffix after the module name.
  8. For instance would resolve relative to where is located.
  9. The suffixed path follows the same module resolution semantics.
  10. Loading from the global folders
  11. If the environment variable is set to a colon-delimited list of absolute paths, then Node.js will search those paths for modules if they are not found elsewhere.
  12. On Windows, is delimited by semicolons instead of colons.
  13. was originally created to support loading modules from varying paths before the current module resolution algorithm was frozen.
  14. is still supported, but is less necessary now that the Node.js ecosystem has settled on a convention for locating dependent modules.
  15. Sometimes deployments that rely on show surprising behavior when people are unaware that must be set. Sometimes a module's dependencies change, causing a different version (or even a different module) to be loaded as the is searched.
  16. Additionally, Node.js will search in the following locations:
  17. Where is the user's home directory, and is Node.js's configured
  18. These are mostly for historic reasons.
  19. It is strongly encouraged to place dependencies in the local folder.
  20. These will be loaded faster, and more reliably.
  21. The module wrapper
  22. Before a module's code is executed, Node.js will wrap it with a function wrapper that looks like the following:
  23. Module code actually lives in here
  24. By doing this, Node.js achieves a few things:
  25. It keeps top-level variables (defined with or) scoped to the module rather than the global object.
  26. It helps to provide some global-looking variables that are actually specific to the module, such as:
  27. The and objects that the implementor can use to export values from the module.
  28. The convenience variables and, containing the module's absolute filename and directory path.
  29. The module scope
  30. The directory name of the current module.
  31. This is the same as the of the
  32. Example: running from
  33. Prints
  34. The file name of the current module. This is the resolved absolute path of the current module file.
  35. For a main program this is not necessarily the same as the file name used in the command line.
  36. See for the directory name of the current module.
  37. Given two modules: and, where is a dependency of and there is a directory structure of:
  38. References to within will return while references to within will return
  39. A reference to the that is shorter to type.
  40. See the section about the exports shortcut for details on when to use and when to use
  41. A reference to the current module, see the section about the
  42. In particular, is used for defining what a module exports and makes available through
  43. To require modules.
  44. Modules are cached in this object when they are required. By deleting a key value from this object, the next will reload the module. Note that this does not apply to native addons, for which reloading will result in an error.
  45. Stability: 0 - Deprecated
  46. Instruct on how to handle certain file extensions.
  47. Process files with the extension as
  48. Deprecated In the past, this list has been used to load non-JavaScript modules into Node.js by compiling them on-demand.
  49. However, in practice, there are much better ways to do this, such as loading modules via some other Node.js program, or compiling them to JavaScript ahead of time.
  50. Since the module system is locked, this feature will probably never go away. However, it may have subtle bugs and complexities that are best left untouched.
  51. Note that the number of file system operations that the module system has to perform in order to resolve a statement to a filename scales linearly with the number of registered extensions.
  52. In other words, adding extensions slows down the module loader and should be discouraged.
  53. The object representing the entry script loaded when the Node.js process launched.
  54. In script:
  55. The module path to resolve.
  56. Paths to resolve module location from.
  57. If present, these paths are used instead of the default resolution paths. Note that each of these paths is used as a starting point for the module resolution algorithm, meaning that the hierarchy is checked from this location.
  58. Use the internal machinery to look up the location of a module, but rather than loading the module, just return the resolved filename.
  59. The module path whose lookup paths are being retrieved.
  60. Returns an array containing the paths searched during resolution of or if the string references a core module, for example or
  61. In each module, the free variable is a reference to the object representing the current module
  62. For convenience, is also accessible via the module-global.
  63. is not actually a global but rather local to each module.
  64. The module objects required by this one.
  65. The object is created by the system.
  66. Sometimes this is not acceptable; many want their module to be an instance of some class. To do this, assign the desired export object to. Note that assigning the desired object to will simply rebind the local variable, which is probably not what is desired.
  67. For example suppose we were making a module called
  68. Do some work, and after some time emit the event from the module itself.
  69. Then in another file we could do:
  70. Note that assignment to must be done immediately. It cannot be done in any callbacks. This does not work:
  71. The variable is available within a module's file-level scope, and is assigned the value of before the module is evaluated.
  72. It allows a shortcut, so that can be written more succinctly as. However, be aware that like any variable, if a new value is assigned to, it is no longer bound to
  73. Exported from require of module
  74. Not exported, only available in the module
  75. When the property is being completely replaced by a new object, it is common to also reassign
  76. To illustrate the behavior, imagine this hypothetical implementation of which is quite similar to what is actually done by
  77. Module code here. In this example, define a function.
  78. At this point, is no longer a shortcut to, and this module will still export an empty default object.
  79. At this point, the module will now export, instead of the default object.
  80. The fully resolved filename to the module.
  81. The identifier for the module. Typically this is the fully resolved filename
  82. Whether or not the module is done loading, or is in the process of loading.
  83. The module that first required this one.
  84. The search paths for the module.
  85. Returns: from the resolved module
  86. The method provides a way to load a module as if was called from the original module.
  87. In order to do this, it is necessary to get a reference to the object. Since returns the, and the is typically only available within a specific module's code, it must be explicitly exported in order to be used.
  88. Provides general utility methods when interacting with instances of — the variable often seen in file modules. Accessed via
  89. A list of the names of all modules provided by Node.js. Can be used to verify if a module is maintained by a third party or not.
  90. Note that in this context isn't the same object that's provided by the module wrapper. To access it, require the module:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement