What is the purpose of Node.js module.exports and how do you use it?

mrwooster

What is the purpose of Node.js module.exports and how do you use it?

I can't seem to find any information on this, but it appears to be a rather important part of Node.js as I often see it in source code.

According to the Node.js documentation:

module

A reference to the current module. In particular module.exports is the same as the exports object. See src/node.js for more information.

But this doesn't really help.

What exactly does module.exports do, and what would a simple example be?

Alnitak

module.exports is the object that's actually returned as the result of a require call.

The exports variable is initially set to that same object (i.e. it's a shorthand "alias"), so in the module code you would usually write something like this:

let myFunc1 = function() { ... };
let myFunc2 = function() { ... };
exports.myFunc1 = myFunc1;
exports.myFunc2 = myFunc2;

to export (or "expose") the internally scoped functions myFunc1 and myFunc2.

And in the calling code you would use:

const m = require('./mymodule');
m.myFunc1();

where the last line shows how the result of require is (usually) just a plain object whose properties may be accessed.

NB: if you overwrite exports then it will no longer refer to module.exports. So if you wish to assign a new object (or a function reference) to exports then you should also assign that new object to module.exports


It's worth noting that the name added to the exports object does not have to be the same as the module's internally scoped name for the value that you're adding, so you could have:

let myVeryLongInternalName = function() { ... };
exports.shortName = myVeryLongInternalName;
// add other objects, functions, as required

followed by:

const m = require('./mymodule');
m.shortName(); // invokes module.myVeryLongInternalName

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use module.exports properly in node.js?

From Dev

What's the use of exports / module.exports?

From Java

Node.js - use of module.exports as a constructor

From Dev

How to reference a variable inside a module.exports in Node.js

From Dev

Node.js/MongoDB: How can I use module.exports to pass a localhost database url to server.js file?

From Dev

How do you use several request handler in node.js?

From Dev

Node.js Express module exports

From Java

module.exports vs exports in Node.js

From Dev

What does Node.js 'require()' return if there is nothing assigned to module.exports

From Dev

How do you set up pretty-error module with a node.js Express app?

From Dev

In Node.js, how do you store callback data from a module's function in the main app?

From Java

What is the purpose of Looper and how to use it?

From Dev

How do you know what relationship to use?

From Dev

How do you know what Node.js code will run on the browser?

From Dev

Why does Node.js' Assert.js use !!!value in their code? What purpose does it serve?

From Dev

Why does Node.js' Assert.js use !!!value in their code? What purpose does it serve?

From Dev

How do you write a node module using typescript?

From Dev

How do you write a node module using typescript?

From Dev

How do you force express on node.js in Azure Websites to use https?

From Dev

How do you use Node.js to stream an MP4 file with ffmpeg?

From Dev

How do you force express on node.js in Azure Websites to use https?

From Dev

How do I use event handlers between JavaScript module files with Node.js?

From Dev

How can I use the Node.js 'async' module to do 'A' or 'B' and then always 'C' in one function?

From Dev

How do I use event handlers between JavaScript module files with Node.js?

From Dev

Different results using Node.js' module.exports in Yeoman

From Java

Declare multiple module.exports in Node.js

From Dev

Different module.exports pattern in node.js

From Dev

Node.js module.exports not exporting the whole object

From Dev

Node JS call a "local" function within module.exports

Related Related

  1. 1

    How to use module.exports properly in node.js?

  2. 2

    What's the use of exports / module.exports?

  3. 3

    Node.js - use of module.exports as a constructor

  4. 4

    How to reference a variable inside a module.exports in Node.js

  5. 5

    Node.js/MongoDB: How can I use module.exports to pass a localhost database url to server.js file?

  6. 6

    How do you use several request handler in node.js?

  7. 7

    Node.js Express module exports

  8. 8

    module.exports vs exports in Node.js

  9. 9

    What does Node.js 'require()' return if there is nothing assigned to module.exports

  10. 10

    How do you set up pretty-error module with a node.js Express app?

  11. 11

    In Node.js, how do you store callback data from a module's function in the main app?

  12. 12

    What is the purpose of Looper and how to use it?

  13. 13

    How do you know what relationship to use?

  14. 14

    How do you know what Node.js code will run on the browser?

  15. 15

    Why does Node.js' Assert.js use !!!value in their code? What purpose does it serve?

  16. 16

    Why does Node.js' Assert.js use !!!value in their code? What purpose does it serve?

  17. 17

    How do you write a node module using typescript?

  18. 18

    How do you write a node module using typescript?

  19. 19

    How do you force express on node.js in Azure Websites to use https?

  20. 20

    How do you use Node.js to stream an MP4 file with ffmpeg?

  21. 21

    How do you force express on node.js in Azure Websites to use https?

  22. 22

    How do I use event handlers between JavaScript module files with Node.js?

  23. 23

    How can I use the Node.js 'async' module to do 'A' or 'B' and then always 'C' in one function?

  24. 24

    How do I use event handlers between JavaScript module files with Node.js?

  25. 25

    Different results using Node.js' module.exports in Yeoman

  26. 26

    Declare multiple module.exports in Node.js

  27. 27

    Different module.exports pattern in node.js

  28. 28

    Node.js module.exports not exporting the whole object

  29. 29

    Node JS call a "local" function within module.exports

HotTag

Archive