JavaScript Importing and Exporting Modules
Last Updated :
15 Dec, 2022
JavaScript Modules are basically libraries which are included in the given program. They are used for connecting two JavaScript programs together to call the functions written in one program without writing the body of the functions itself in another program.
Importing a library: It means include a library in a program so that use the function is defined in that library. For this, use 'require' function in which pass the library name with its relative path.
Example: Suppose a library is made in the same folder with file name library.js, then include the file by using require function:
const lib = require('./library')
which will return a reference to that library. Now if there is an area function defined in the library, then use it as lib.area().
Exporting a library: There is a special object in JavaScript called module.exports. When some program include or import this module (program), this object will be exposed. Therefore, all those functions that need to be exposed or need to be available so that it can use in some other file, defined in module.exports.
Example: In this example, we will write two different programs and then see how to use functions defined in the library (Module) in a given program. We will define two simple functions in the library for calculating and printing area and perimeter of a rectangle when provided with length and breadth. Then export the functions so that other programs can import them if needed and can use them.
Exporting Module Example: library.js
JavaScript
<script>
let area = function (length, breadth) {
let a = length * breadth;
console.log('Area of the rectangle is ' + a + ' square unit');
}
let perimeter = function (length, breadth) {
let p = 2 * (length + breadth);
console.log('Perimeter of the rectangle is ' + p + ' unit');
}
module.exports = {
area,
perimeter
}
</script>
Importing Module Example
For importing any module, use a function called 'require' which takes in the module name, and if it is a user-defined module then its relative path as an argument and returns its reference.
The script.js contains the above JavaScript module (library.js).
Script.js
JavaScript
<script>
const lib = require('./library');
// " ./ " is used if both the files are in the same folder.
let length = 10;
let breadth = 5;
lib.area(length, breadth);
lib.perimeter(length, breadth);
</script>
Output:
Area of the rectangle is 50 square unit
Perimeter of the rectangle is 30 unit
To learn about new methods for exporting and importing in ES6. Click here
Note: To run the script first make both files in the same folder and then run script.js using NodeJs interpreter in the terminal.
Similar Reads
Import and Export in Node.js Importing and exporting files are important parts of any programming language. Importing functions or modules enhances the reusability of code. When the application grows in size, maintaining a single file with all the functions and logic becomes difficult. It also hinders the process of debugging.
3 min read
React Importing and Exporting Components In React, components are the building blocks of any application. They allow developers to break down complex user interfaces into smaller, reusable pieces. To manage these components efficiently, you need to understand how to import and export them.In this article, we will explore the different meth
6 min read
What is export default in JavaScript ? JavaScript modules allow you to organize code into separate files, making it easier to maintain and reuse. To share code between modules, JavaScript provides two types of exports: Named Exports and Default Exports. Understanding how and when to use these export types is key to effectively managing m
3 min read
JavaScript Modules In JavaScript, modules are a fundamental feature for organizing, structuring, and reusing code. A module is simply a chunk of code that can be reused in different parts of a program or even in different programs. JavaScriptâs modularity is key to managing complexity, especially for large application
8 min read
Modular Scope in JavaScript Modular scope in JavaScript refers to the practice of creating isolated scopes within modules, ensuring that variables and functions are not exposed globally unless explicitly intended. This approach helps prevent name conflicts and encourages better organization of code.Modules allow you to define
6 min read
Differece between JavaScript Modules & NgModule Modules play an important role in structuring and organizing the code of Javascript and Angular.js. They help us to use the code efficiently. Majorly NgModule in Angular.js and Modules in Javascript are widely used in different scenarios. In this article, We will learn about Modules and their import
5 min read