1

I was considering updating my Node/Express code to use import instead of require by adding to my package.json file the line:

  "type": "module",

However, I did not know why most Node/Express code I see, still uses require while code I see in the browser via webpack uses import.

Is there a reason for this duality? Is upgrading to import recommended in general? Is import because it is newer considered an upgrade and a better technology?

This previous Q/A is over 7 years old and has lot of historical information. I am looking for more current information as the language has evolved.

favor
  • 355
  • 2
  • 13
  • "why most Node/Express code I see, still uses require" because node has been around for many years, and `require` is how it's been done since forever - changing the default would break the internet – Bravo Feb 28 '22 at 05:22

1 Answers1

1

The reason for having two types of modules is purely historical.

Back in 2009 when Node.js was created, ES modules (import syntax) did not exist yet, but there existed other nonstandard alternatives, with the most important being AMD and CommonJS, for which several browser implementations were available. Node.js decided to adopt CommonJS (require syntax). For this reason, all packages deployed up until 2015 are CommonJS packages.

Native ES modules were introduced with the ECMAScript 2015 standard, but they only made it as an experimental technology in Node.js 8.5 in 2017, and it wasn't until Node.js 12 (2019) that their support became good enough to place them as an alternative to CommonJS (although this is subjective). For this reason, a large part of software developed for Node.js still uses CommonJS modules with require statements.

Whether ES modules are better is a matter of opinion, and not a proper question for StackOverflow. At least, they are standard JavaScript, which makes them fit for use both in browsers and in Node.js, which is imo a big advantage.

A disadvantage is that ES modules cannot be required in CommonJS code - the opposite is true: CommonJS modules can be imported by ES modules - so if any of your dependencies uses ES modules, that may be a good opportunity to switch your code to ES modules, too.

So should we upgrade to use import in general? Of course, that's also a matter of opinion. And if that was my opinion, the answer would be "no", because there is no reason to change a running system. CommonJS code is not going to rust or worsen simply because a better alternative exists nowadays, although ES modules are a better choice for new code. Node.js, as long as people use it, is not going to drop support for CommonJS either.

There is a project called Deno which is designed to provide a more modern and secure alternative to Node.js to run JavaScript code on the server. Deno was originally designed to work with ES modules only, but it soon became clear that not supporting CommonJS modules was a major issue, because so many npm packages were not working, so it introduced a compatibility mode to support CommonJS modules under a flag. The moral of the story is: removing support for require is good idea, but re-adding it is an even better one.

GOTO 0
  • 42,323
  • 22
  • 125
  • 158