What is the difference between require() and import?

require() is the CommonJS module-loading mechanism, while import is the ES Module syntax. They differ in module semantics, resolution behavior, loading characteristics, and configuration. Modern Node.js supports interoperability between the systems, but mixing them without understanding the module configuration can lead to compatibility issues.

Key Takeaways
  • require() belongs to CommonJS.
  • import belongs to ESM.
  • Avoid inconsistent module configuration.
Knowledge Check

Which statement is correct?