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 particularmodule.exports
is the same as the exports object. Seesrc/node.js
for more information.
But this doesn't really help.
What exactly does module.exports
do, and what would a simple example be?
the refer link is like this:
the properties of
exports
ormodule.exports
,such as functions or variables , will be exposed outsidethere is something you must pay more attention : don't
override
exports .why ?
because exports just the reference of module.exports , you can add the properties onto the exports ,but if you override the exports , the reference link will be broken .
good example :
bad example :
If you just want to exposed only one function or variable , like this:
this module only exposed one function and the property of name is private for the outside .
module.exports
is the object that's actually returned as the result of arequire
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:to export (or "expose") the internally scoped functions
myFunc1
andmyFunc2
.And in the calling code you would use:
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 tomodule.exports
. So if you wish to assign a new object (or a function reference) toexports
then you should also assign that new object tomodule.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:followed by:
There are some default or existing modules in node.js when you download and install node.js like http, sys etc.
Since they are already in node.js, when we want to use these modules we basically do like import modules, but why? because they are already present in the node.js. Importing is like taking them from node.js and putting them into your program. And then using them.
Whereas Exports is exactly the opposite, you are creating the module you want, let's say the module addition.js and putting that module into the node.js, you do it by exporting it.
Before I write anything here, remember, module.exports.additionTwo is same as exports.additionTwo
Huh, so that's the reason, we do like
Be careful with the path
Lets say you have created an addition.js module,
When you run this on your NODE.JS command prompt:
This will error out saying
This is because the node.js process is unable the addition.js since we didn't mention the path. So, we have can set the path by using NODE_PATH
Now, this should run successfully without any errors!!
One more thing, you can also run the addition.js file by not setting the NODE_PATH, back to your nodejs command prompt:
Since we are providing the path here by saying it's in the current directory
./
this should also run successfully.A module encapsulates related code into a single unit of code. When creating a module, this can be interpreted as moving all related functions into a file.
Suppose there is a file Hello.js which include two functions
We write a function only when utility of the code is more than one call.
Suppose we want to increase utility of the function to a different file say World.js,in this case exporting a file comes into picture which can be obtained by module.exports.
You can just export both the function by the code given below
Now you just need to require the file name into World.js inorder to use those functions
It accomplishes the following things:
Having modules makes it easier to find certain parts of code which makes our code more maintainable.
NodejS
uses the CommomJS module system which works in the following manner:module.export
syntaxrequire('file')
syntaxExample:
test1.js
test2.js
Other useful things to know:
require()
is called on the same module the is pulled from the cache.require()
right away.