'Is module.exports the same as export [duplicate]
Lets say you are building a npm library and you would like to export your functions.Like so:
function a(){
}
And now you want to export them. A way to do that locally would be:
export function a(){
}
But you could do it like this:
function a(){
}
module.exports = a;
Whats the difference? Do they do the same thing?
Solution 1:[1]
no it's not the same,
module.exports is cjs syntax while import/export is modern esm syntax
cjs is older than esm, so the later is preferrable
with cjs you use require to import it
In the end if you write modern es6 javascript always use esm
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |