What is the difference between ES modules and CommonJS?
CommonJS is the older system, and it is the one that Node was built on. It works at runtime. You call the require function, it gives you back an exports object, and the code runs immediately as it is required. Every decision about the module is made while the program is running.
ES modules are the standard system. The import and export keywords are static, so the whole graph of modules is worked out before any code runs. The engine knows exactly what each module imports and exports without executing anything. That is what makes tree shaking possible, and it is also what lets the engine refuse to load a module whose import name is spelled wrong.
Five differences come up in interviews. Imports are hoisted, so you can use an imported name before the import statement appears in the file. Exports are live views, so a change to an exported variable is visible to the files that imported it without re-importing, while the CommonJS exports object is a snapshot taken at one moment. CommonJS can export a value conditionally or inside a loop, and ES modules cannot do that, because the export list is fixed when the file is parsed. And ES modules support a top level await, which lets a module wait for data before it finishes loading.
There is a practical consequence as well. CommonJS is synchronous by default, so requiring a heavy module blocks the event loop while it loads. Loading an ES module is asynchronous, so a large graph does not stop the server in quite the same way.