ES6
September 10, 2026 6 min read

Module in ES6

HS
HEMANT SINGH
Technical Writer & Educator

JavaScript Module

Hemant Singh

Technical Writer & Educator

A module is simply a JavaScript file that contains code that can be reused in another JavaScript file.

Modules help us divide a large JavaScript application into smaller and manageable files.

For example:

data.js
home.js
user.js
product.js

Each file can contain a specific part of our application.

Module Uses Two Main Directives

JavaScript modules mainly use two keywords:

  1. export
  2. import

export

export enables us to make variables, arrays, objects, functions, classes, etc. available to other JavaScript files.

import

import enables us to use exported code from another JavaScript file.

Using JavaScript Modules

There are different ways to use JavaScript modules.

One popular way is to use JavaScript modules with Node.js.

For Node.js, we can use a package.json file and specify:

"type": "module"

For example:

package.json

{
"name": "es6",
"version": "1.0.0",
"description": "",
"main": "data.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1
},
"author": "",
"license": "ISC",
"type": "module"
}

The important part is:

"type": "module"

This tells Node.js to treat .js files as ES Modules.

Export and Import Variable

We can export a variable from one JavaScript file and import it into another file.

data.js

export let company = "CAPSCOM";

Here:

export

makes the company variable available to other modules.

home.js

import { company } from './data.js';

console.log(company);

Output:

CAPSCOM

The { } are used because company is a named export.

Export and Import Array

We can also export an array.

data.js

export let products = ["Mobile", "TV", "Mouse"];

home.js

import { products } from './data.js';

console.log(products);

Output:

["Mobile", "TV", "Mouse"]

We can also access individual elements:

console.log(products[0]);

Output:

Mobile

Export and Import Object

We can export an object from one module.

data.js

let person = {
name: "Ajay",
age: 20
};

export { person };

Here:

export { person };

exports the person object.

home.js

import { person } from './data.js';

console.log(person);

Output:

{ name: "Ajay", age: 20 }

We can access object properties:

console.log(person.name);
console.log(person.age);

Output:

Ajay
20

Export and Import Function

We can also export a function.

data.js

let sum = (a, b) => a + b;

export { sum };

home.js

import { sum } from './data.js';

console.log(sum(10, 20));

Output:

30

The function is created in data.js but can be used in home.js.

Export Multiple Items

We can export multiple variables, arrays, objects, and functions from the same file.

data.js

let company = "CAPSCOM";

let products = ["Mobile", "TV", "Mouse"];

let person = {
name: "Ajay",
age: 20
};

let sum = (a, b) => a + b;

export {
company,
products,
person,
sum
};

Now we can import all of them.

home.js

import {
company,
products,
person,
sum
} from './data.js';

console.log(company);

console.log(products);

console.log(person);

console.log(sum(10, 20));

Output:

CAPSCOM

["Mobile", "TV", "Mouse"]

{ name: "Ajay", age: 20 }

30

Named Export

When we export something using its name:

export let company = "CAPSCOM";

or:

let company = "CAPSCOM";

export { company };

it is called a named export.

When importing a named export, we use { }.

import { company } from './data.js';

The imported name normally needs to match the exported name.

For example:

export let company = "CAPSCOM";

Then:

import { company } from './data.js';

Module as a Single Task

Sometimes a module is created mainly to provide one important value or function.

For this situation, we can use:

export default

A module can have one default export.

For example, suppose our data.js file mainly provides a sum() function.

data.js

let sum = (a, b) => a + b;

export default sum;

Now we can import it without { }.

home.js

import sum from './data.js';

console.log(sum(10, 20));

Output:

30

Named Export vs Default Export

There is an important difference between named export and default export.

Named Export

export let company = "CAPSCOM";

Import:

import { company } from './data.js';

We use { }.

Default Export

let sum = (a, b) => a + b;

export default sum;

Import:

import sum from './data.js';

We do not use { }.

Default Export Can Have a Different Name

With a default export, the name used while importing does not have to be the same.

For example:

data.js

let sum = (a, b) => a + b;

export default sum;

We can write:

import addition from './data.js';

console.log(addition(10, 20));

Output:

30

We could also write:

import calculate from './data.js';

console.log(calculate(10, 20));

Both work because it is a default export.

Import Multiple Named Exports

Suppose data.js contains:

export let company = "CAPSCOM";

export let age = 20;

export let sum = (a, b) => a + b;

We can import all three:

import {
company,
age,
sum
} from './data.js';

console.log(company);

console.log(age);

console.log(sum(10, 20));

Output:

CAPSCOM
20
30

Rename an Import

We can also give a different name to a named import using as.

data.js

export let company = "CAPSCOM";

home.js

import { company as companyName } from './data.js';

console.log(companyName);

Output:

CAPSCOM

Here:

company as companyName

means:

Import company but use it as companyName in this file.

Import Everything

We can import all named exports using *.

data.js

export let company = "CAPSCOM";

export let age = 20;

export let sum = (a, b) => a + b;

home.js

import * as data from './data.js';

console.log(data.company);

console.log(data.age);

console.log(data.sum(10, 20));

Output:

CAPSCOM
20
30

Here:

* as data

means:

Import all named exports and keep them inside the data object.

Why Do We Use Modules?

Suppose we have a large application.

Without modules, we may put everything into one file:

app.js

The file can become very large and difficult to manage.

With modules, we can divide our code:

data.js
user.js
product.js
cart.js
payment.js
app.js

For example:

user.js
↓
User related code

product.js
↓
Product related code

cart.js
↓
Cart related code

payment.js
↓
Payment related code

This makes our application:

  1. Easier to understand
  2. Easier to maintain
  3. Easier to reuse
  4. Easier to debug
  5. Better organized

Important Points to Remember

1. Module is a JavaScript file

data.js

can be a module.

2. export sends code out of a module

export { company };

3. import brings code into another module

import { company } from './data.js';

4. Named exports use { }

export { company };
import { company } from './data.js';

5. Default export does not use { }

export default sum;
import sum from './data.js';

6. A module can have only one default export

export default sum;

7. A module can have multiple named exports

export let company = "CAPSCOM";

export let age = 20;

export let sum = (a, b) => a + b;

Quick Revision

JavaScript Module
|
---------------------
| |
export import
| |
Send code out Bring code in
| |
↓ ↓
Other JavaScript files

Named Export

export let company = "CAPSCOM";

Import:

import { company } from './data.js';

Default Export

let sum = (a, b) => a + b;

export default sum;

Import:

import sum from './data.js';

Multiple Exports

export {
company,
products,
person,
sum
};

Import:

import {
company,
products,
person,
sum
} from './data.js';

JavaScript modules allow us to divide our application into separate files and reuse code wherever it is required.