The require()` and import statements serve similar purposes but have some key differences:
CommonJS Syntax: require('module')
Synchronous: It loads modules one after another, blocking execution until the module is loaded.
Supports Both CommonJS and ES Modules: You can use it to import both CommonJS and ES modules.
No Explicit Default Export Support: It doesn't explicitly support default exports.
Named Exports via Destructuring: You can use destructuring to import named exports.
No Static Analysis (Tree Shaking): It doesn't enable tree shaking.
No Dynamic Importing: It doesn't support dynamic importing.
Used Primarily in Node.js.
ES6 Syntax: import module from 'module'
Asynchronous (Lazy Loading): It loads modules only when needed, improving performance.
Supports Only ES Modules: Requires .mjs extension for ES modules.
Explicit Default Export Support: You can directly import default exports.
Named Exports Directly: You can import named exports directly.
Static Analysis: Enables tree shaking.
Dynamic Importing Supported: You can use the import() function.
Used in Modern JavaScript (Browser and Node.js).