CommonJS vs ES Modules in JS
Apart from that,
You canβt selectively load only the pieces you need with require but with import, you can selectively load only the pieces you need, which can save memory.
Loading is synchronous(step by step) for require on the other hand import can be asynchronous(without waiting for previous import) so it can perform a little better than require.
.cjs (CommonJS):
- Explicitly marks a file as CommonJS, regardless of the
package.jsonconfiguration. - Useful when working with both module systems in the same project.
.mjs (ES Modules):
- Explicitly marks a file as an ES module, regardless of the
package.jsonconfiguration. - Avoids ambiguity when no
package.jsonis present or when the file is standalone.
Study Middleware by piyush garg. He said middleware are like plugins
