0

It seems like ES6 imports for Node have been a bugbear for a while. There is the --experimental-modules flag, or options like using Babel - but AFAIK ES6 modules still aren't supported in vanilla Node.js.

Meanwhile, ES6 modules are supported by all the major browsers.

Why is that the browsers have been able to implement them, but Node hasn't?

closed as primarily opinion-based by gnat, Thomas Owens Sep 15 '18 at 11:46

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

4

Essentially, it boils down to how to get two very different module systems to work together. CommonJS (CJS) Modules are dynamic and synchronously loaded, ES Modules are static, immutable and asynchronously loaded. But no one wants to break the Node ecosystem, meaning that we want to be able to load CJS modules from ES modules and vice versa, ideally without the user having to know which kind of modules a package uses. (Currently you can't load ES modules from CJS modules, that will require a new asynchronous loading function.) Some people also want to be able to publish packages with exports in both kind of module.

Browsers didn't have any kind of modules previously, so they didn't have to make ES modules cooperate with anything. They could just do a simple implementation.

Any module system across such different environments as browsers and Node is going to have some tricky edge cases. Even though with the --experimental-modules flag Node's ES Modules implementation is largely the same as a browser's, there are still differences. For example, browsers will only load from full URLs, but Node will load packages from node_modules with only a package name.

  • "no one wants to break the Node ecosystem." Speak for yourself. There's plenty of developers who would love nothing more than to see it die out! – Mason Wheeler Sep 15 '18 at 13:46
  • "Browsers didn't have any kind of modules previously, so" ... they decided to implement something purposefully imcompatible that complicates any integration with an existing module system CommonJS/NodeJS (because of [NIHS])(en.wikipedia.org/wiki/Not_invented_here) ). – humanityANDpeace Oct 1 '18 at 14:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.