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:
Each file can contain a specific part of our application.
Module Uses Two Main Directives
JavaScript modules mainly use two keywords:
exportimport
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:
For example:
package.json
The important part is:
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
Here:
makes the company variable available to other modules.
home.js
Output:
The { } are used because company is a named export.
Export and Import Array
We can also export an array.
data.js
home.js
Output:
We can also access individual elements:
Output:
Export and Import Object
We can export an object from one module.
data.js
Here:
exports the person object.
home.js
Output:
We can access object properties:
Output:
Export and Import Function
We can also export a function.
data.js
home.js
Output:
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
Now we can import all of them.
home.js
Output:
Named Export
When we export something using its name:
or:
it is called a named export.
When importing a named export, we use { }.
The imported name normally needs to match the exported name.
For example:
Then:
Module as a Single Task
Sometimes a module is created mainly to provide one important value or function.
For this situation, we can use:
A module can have one default export.
For example, suppose our data.js file mainly provides a sum() function.
data.js
Now we can import it without { }.
home.js
Output:
Named Export vs Default Export
There is an important difference between named export and default export.
Named Export
Import:
We use { }.
Default Export
Import:
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
We can write:
Output:
We could also write:
Both work because it is a default export.
Import Multiple Named Exports
Suppose data.js contains:
We can import all three:
Output:
Rename an Import
We can also give a different name to a named import using as.
data.js
home.js
Output:
Here:
means:
Importcompanybut use it ascompanyNamein this file.
Import Everything
We can import all named exports using *.
data.js
home.js
Output:
Here:
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:
The file can become very large and difficult to manage.
With modules, we can divide our code:
For example:
This makes our application:
- Easier to understand
- Easier to maintain
- Easier to reuse
- Easier to debug
- Better organized
Important Points to Remember
1. Module is a JavaScript file
can be a module.
2. export sends code out of a module
3. import brings code into another module
4. Named exports use { }
5. Default export does not use { }
6. A module can have only one default export
7. A module can have multiple named exports
Quick Revision
Named Export
Import:
Default Export
Import:
Multiple Exports
Import:
JavaScript modules allow us to divide our application into separate files and reuse code wherever it is required.