News for hackers. Stuff that matters.
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge... by indeyets on Thursday August 06, 2020 @05:13AM (#24069329)
Sorry about nitpicking, but I feel that we shouldn't say that CJS has named exports. It does not.

CJS has a single export which might be an object. And objects have named properties/fields.

On the other hand, ESM CAN have unnamed "default" export and CAN have arbitrary number of named exports.

Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge... by DaiPlusPlus on Thursday August 06, 2020 @06:49AM (#24069780)
> On the other hand, ESM CAN have unnamed "default" export and CAN have arbitrary number of named exports.

And this is a constant source of confusion and frustration for me when working with so many npm modules lately...

Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge... by jkrems on Thursday August 06, 2020 @01:23PM (#24073154)
> On the other hand, ESM CAN have unnamed "default" export and CAN have arbitrary number of named exports.

To nitpick on the nitpick: ESM only has named exports. It just treats the export named "default" differently in some scenarios and has special syntax for it.

So, roughly:

* CJS: Exports a single value which may or may not be an object. When assigning the exported value to local variables, destructuring can be used to access individual exports properties.

* ESM: Exports a namespace of named bindings. When importing the bindings to local aliases, the name of the alias can be changed using `as`. There's special syntax for importing the binding with the name "default".

It's somewhat unfortunate that the syntax _looks_ so close without being all that close.