daily pastebin goal


a guest Jun 23rd, 2018 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Node.js module.exports vs. exports
  2. What are they, how to use them and how not to use them
  3. (Note that this article was written after the Node.js 6.1.0 release)
  5. TL;DR
  6. Think of module.exports as the variable that gets returned from require(). It is an empty object by default, and it is fine to change to anything.
  7. Exports? Well, “exports” itself is never returned! It is just a reference to module.exports; a convenience variable to help module authors write less code. Working with its properties is safe and recommended.
  8. exports.method = function() {…}
  9. vs.
  10. module.exports.method = function() {…}
  11. A simple module example
  12. First, we need an example codebase. Let’s start with a simple calculator:
  15. calculator.js
  16. Usage:
  19. app-use-calculator.js
  20. The module wrapper
  21. Node.js internally wraps all require()-ed modules in a function wrapper:
  24. calculator-wrapped.js
  25. The module object
  26. Variable “module” is an object representing the current module. It is local to each module and it is also private (only accessible from module code):
  29. calculator-printed.js
  30. Module.exports
  31. It is the object reference that gets returned from the require() calls.
  32. It is automatically created by Node.js.
  33. It is just a reference to a plain JavaScript object.
  34. It is also empty by default (our code attaches an “add()” method to it)
  35. There are two ways we can use module.exports:
  37. Attaching public methods to it (like we did in the calculator example).
  38. Replacing it with our custom object or function.
  39. Why replace it? When replacing, we can return any arbitrary instance of some other class. Here is an example written in ES2015:
  42. calculator-base.js
  43. Above, “calculator-base” exports a class.
  44. Let’s extend “Calculator” class and export an instance this time:
  47. calculator-advanced.js
  48. Usage:
  51. app-use-advanced-calculator.js
  52. Exports alias
  53. “exports” is just a convenience variable so module authors can write less code
  54. Working with its properties is safe and recommended.
  55. (eg.: exports.add = function…)
  56. Exports is NOT returned by require() (module.exports is!)
  57. Here are some good and some bad examples:
  60. Note: It is common practice to replace module.exports with custom functions or objects. If we do that but still would like to keep using the “exports” shorthand; then “exports” must be re-pointed to our new custom object (also done in code above at line 12):
  62. exports = module.exports = {}
  63. exports.method = function() {...}
  64. Conclusion
  65. A variable named exports that is not being entirely exported is confusing, especially for newcomers to Node.js. Even the official documentation has a slightly strange take on it too:
  67. As a guideline, if the relationship between exports and module.exports seems like magic to you, ignore exports and only use module.exports.
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand