28/05/2026
π ES6 Modules vs CommonJS Modules in JavaScript
If you're learning backend or modern JavaScript, you've probably seen these two ways of importing files π
π CommonJS Module
(Used mostly in older Node.js projects)
Export:
```js id="r3k7n1"
module.exports = add
```
Import:
```js id="v6m2x8"
const add = require("./math")
```
π ES6 Module (ESM)
(Modern JavaScript standard)
Export:
```js id="b9q4p5"
export default add
```
Import:
```js id="c2z8w6"
import add from "./math.js"
```
β‘ Main Difference
β
CommonJS uses:
`require()` & `module.exports`
β
ES6 Modules use:
`import` & `export`
π Why ES6 Modules are becoming popular?
β Cleaner syntax
β Better readability
β Supported in modern browsers
β Official JavaScript standard
β Better for modern frameworks
π Enable ES6 Modules in Node.js
Add this in `package.json`
```json id="y7f1k3"
{
"type": "module"
}
```
π‘ CommonJS is still widely used in older Node.js applications, but ES6 Modules are the future of JavaScript development.
If youβre starting today, learn both β but focus more on ES6 Modules π