Current Javascript adopts import from ES6 as a standard way to import modules.
However, I sometimes see codes using CommonJS require instead of import.
I first wondered whether two can be used together, but it seems like two are not interchangeable. (releated stackoverflow question)
So, is CommonJS ‘require’ something that is still used in projects? Or is it slowly dying and needed only for maintaining legacy codes?
>Solution :
CommonJS will likely be supported in nodejs for a long time as there are still millions of lines of code written using it. It is the original module loading mechanism in nodejs.
ESM modules (ECMAScript modules) that use import and export are the new Javascript standard for modules and we can expect that nodejs will support these for as long as they are the Javascript standard (probably forever).
These two modules standards are not entirely compatible so mixing and matching within the same project can lead to difficulties.
If you were starting a project today with a clean slate and no dependencies on any existing modules, you would most likely choose to write your code using ESM modules as it is the future trajectory of the language and nodejs. And, it’s possible to write modules that can be used as native modules in both nodejs and the browser as both now support ESM modules.
But, life is not quite that simple. One of the things that has made nodejs such an enormous success is the massive library of open source modules on NPM for solving all sorts of problems. Most of these modules started their lives as CommonJS modules (the original nodejs module format). Some have been updated so that they can be loaded either as a CommmonJS module or as an ESM module (yes, it’s possible to create a module that can be loaded either way). But, many have not.
So, if you are starting a new project, you need to take a quick survey of the most important modules you will depend on and figure out if they support ESM loading or not. If some critical module does not, then you may need to go with a CommonJS project or start learning about the work-arounds for cross module-type loading and see if that will work in your circumstance.
Over time, I would expect that any actively developed, shared module on NPM will eventually support ESM loading. But, we’re in this transition period of time where many have not yet implemented that or there are particular challenges in implementing the new loading scheme (which comes with it’s own and different set of rules).
One of the important interoperability things to know is that you can load a CommonJS module from an ESM module with a dynamic import. This is a function-like import that is not synchronous (it returns a promise that resolves to the module’s exports).
It’s also useful to know that ESM modules do not have some nodejs niceties that CommonJS modules had. There’s no automatic __dirname or __filename to tell you exactly where this file was loaded from. Instead, you have to parse those values out of import.meta.url and compute them. So, it’s still possible to get that information, it’s just not as convenient as it used to be.