What is the difference between exports and module.exports




















There is strictly not a need for exports , it's just a handy shorthand. But now it can never be removed because it would break all existing code. Show 1 more comment. Renee's answer is well explained. Suppose you have greet. Now, imagine you re-write greet. Now, imagine you re-write this is called Mutation greet. Conclusion is always use module.

Hope this helps. Happy coding :. Sdembla Sdembla 1, 11 11 silver badges 13 13 bronze badges. This too, is a beautiful insightful answer and complements goto-bus-stop 's answer. Also, one things that may help to understand: math. However, if you change your implementation to: math. Another way to do it would be: math.

Rodrigo Branas Rodrigo Branas 5 5 silver badges 9 9 bronze badges. Siderite Zackwehdex 5, 2 2 gold badges 25 25 silver badges 41 41 bronze badges. I became confused with this explanation, kind to elaborate? GuyFreakz I'm not sure if this speaks to your confusion, but module. If you change what one variable references, the two variables no longer reference the same thing.

The line of code above ensures both variables are initialized to the same new object. An actual use case which everyone else missed out on fengshuo. This is done just for convenience. Ankit Aabad Ankit Aabad 65 1 1 silver badge 4 4 bronze badges. Shashwat Gupta Shashwat Gupta 3, 30 30 silver badges 25 25 bronze badges. They implemented CommonJS specification to achieve this.

The two important pieces of the puzzle are module. This is how the module object looks like when we console. The above object basically describes an encapsulated module from a JS file with module.

Default exporting in a Node. There's another way of exporting from a Node. Instead of assigning the whole module. Something like this:. Named export can also be done more concisely with the module-scoped exports predefined variable, like this:. However, assigning the whole exports variable to a new value won't work we will discuss why in a later section , and often confuses Node.

Imagine that Node. Well, "default export" would be telling module. An important point to note about this foreign port operator — require — is that the person is adamant about receiving ships that were sent by module. This leads us to the next section where we will address a common point of confusion. Renee's answer is well explained. Suppose you have greet. Now, imagine you re-write greet. Now, imagine you re-write this is called Mutation greet.

Conclusion is always use module. Hope this helps. Happy coding : Also, one things that may help to understand: math. However, if you change your implementation to: math. Well, if you look at the user. Then in the index.

Also notice that in the require statement, the module name is prefixed with. Notice how the name we give the exported dateOfBirth variable can be anything we fancy dob in this case. And thanks to destructuring assignment , we can cherry-pick what we want to import:. So what is the difference between module. Is one just a handy alias for the other?



0コメント

  • 1000 / 1000