Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

284
Visualizações
What's the benefit of using ES6 module syntax in Node.js without transpilation?

As title. Since Node.js now supports ES6 module, you can simply add "type": "module" in your package.json to facilitate the import/export syntax even without the --experimental-modules flag to run your code via the node CLI-command without any problem. To me:

  1. This also implies that we actually don't need to transpile our ES6 module syntax to those require() from CommonJS spec in our code.
  2. But in Node.js v16.x documentation, they explicitly list some differences between the specs of both CommonJS (module) and ECMAScript module (loaders). So it's actually NOT as simple as doing the same thing differently. There must be some trade-offs to prefer one over the other, and this is what I want to know!

So why do some people want to use ES6 module? Is it only for importing modules asynchronously? I don't think so. Their most be some reasons more important than this. But apparently moving toward the newest syntax will be a trend. Any idea?


p.s. I've read some old threads saying that most testing frameworks haven't supported ES6 module syntax, and from the last point on the list of CommonJS:

It cannot be used to load ECMAScript modules (although it is possible to load ECMASCript modules from CommonJS modules).

So I believe that this could be one of the reasons most of the people don't care about migrating to the newest import syntax on Node.js.

Can anyone please correct me if some statement(s) I provided above is wrong?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The comment section under my OP has become quite big. So let me try to provide a conclusion for myself(and the future readers), after many tabs(probably over 100) now got closed after I (finally) had organized them.


What's the benefit of using ES6 module syntax in Node.js without transpilation?

The "without transpilation" means that the version of Node.js you're using has to support ES6 module, which it does since v12, as per @Felix Kling's answer.


[...] So it's actually NOT as simple as doing the same thing differently.

But actually you can import CommonJS modules without problems, as per an article by Simon Plenderleith, which includes a link to the related pull request in Node.js repository. This is also mentioned by @jabaa under the comment section of my OP. But unfortunately he/she doesn't have a reference for it.


So why do some people want to use ES6 module? Is it only for importing modules asynchronously? I don't think so. Their most be some reasons more important than this. [...]

Then one of the reason must be tree-shaking, which is mentioned twice in the comment section under my OP. And notice that while this might be possible on CommonJS modules, the performance of it with ES6 modules must be better, since the syntax-level ES6 import is static in nature(I'm not talking about the import() version), and ES6 modules will only be initialized once. With tree-shaking, your code gets smaller thus your website/library will run/load faster, and speed is important at front-end, no?


But apparently moving toward the newest syntax will be a trend. Any idea?

Absolutely. At the time I'm writing this, Node.js is default to "type": "commonjs" because of compatibility, since during the old days ES6 modules syntax has not been merged/invented. Now using ES6 modules syntax is future-proof, it will work even when people will not use Node.js in the future(sorry about saying this! But apparently it will probably be replaced by something new/fancy in the future...)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda