0

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?

DcraftBg
  • 27
  • 5
  • @evolutionxbox i know its a module thingy but do these two things to the same thing or are they different? – DcraftBg Apr 29 '22 at 16:29
  • `module.exports` for example, is called "commonJS". Its usually used in Node.js. on the other hand `export` etc. is used on the frontend. You see it in Vue, React, etc. If you use typescript you usually use module instead of commonJs – bill.gates Apr 29 '22 at 16:38

1 Answers1

2

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

Lk77
  • 2,203
  • 1
  • 10
  • 15