Importing and Exporting Modules in JavaScript

JavaScript | Importing and Exporting Modules: In this tutorial, we are going to learn how to import and export the modules in JavaScript?
Submitted by Siddhant Verma, on November 27, 2019

We have already seen what Modules are in JavaScript, where they are needed and why and when we should use them. In this article, we'll dive further into the JavaScript Modules and use NodeJS to export and import modules for our application. When we work with modules in NodeJS we can imagine them as libraries that we can import and export so that their code can be used in another file inside the same application.

const myModule=require('./myModule');

Let's say you created a module named myModule which contains some JavaScript code inside it and you want it use it inside your root JS file, say app.js. You can import this module using the "require" keyword which will return a reference to the script.

We can export modules using a special JavaScript object called module.exports. This allows other files to read this file by importing or requiring them first. All the functions, variables and objects that your module contains can now be used by any other file once it imports it.

Let's look at an example. Make sure you have the latest stable version of Node.js installed. Create two files, let's call our main file index.js and the other script module.js.

module.js:

let pokemons=['Squirtle',
'Pikachu',
'Bulbasaur',
'Meowth'];
let attributes= {
    HP: 100, HomeTown: 'Pokeland', isEvolved: false
}

module.exports= {
    pokemons,
    attributes
}

Inside our index.js, let's import this module and log something to the console,

const pokemodule =  require('./module'); 
console.log(pokemodule.pokemons);

Command line: node index

Output

[ 'Squirtle', 'Pikachu', 'Bulbasaur', 'Meowth' ]

We imported a module inside our script and use a variable that should not have been accessible in that script otherwise. This is a basic example of how to import and export modules in JavaScript. Let's take it further to build something useful. Imagine we're building a calculator application but we have no idea what plus-minus means. Thankfully we have a fellow dev who's also skilled at math so he writes down some basic arithmetic functions for us inside a module. Here's what he wrote looks like,

calc.js:

let add = (a, b) => a + b;
let subtract = (a, b) => a - b;
let multiply = (a, b) => a * b;
let divide = (a, b) => a / b;
let remainder = (a, b) => a % b;
module.exports = {
    add,
    subtract,
    multiply,
    divide,
    remainder
}

Now all we got to do is import it inside our main file and invoke its methods and display those results and we can do this because he exported that module giving us the ability to import it and use the variables, functions defined inside that module,

index.js:

const calc =  require('./calc'); 
let a=10, b=5;
let sum=calc.add(a,b);
let diff=calc.subtract(a,b);
let prod=calc.multiply(a,b);
let mod=calc.remainder(a,b);

console.log(`${a} + ${b} = ${sum}`);
console.log(`${a} - ${b} = ${diff}`);
console.log(`${a} X ${b} = ${prod}`);
console.log(`${a} % ${b} = ${mod}`);

Let's run our index.js file using Node Js,

Command line: node index

Output

10 + 5 = 15
10 - 5 = 5
10 X 5 = 50
10 % 5 = 0

This is a basic example of a use case where we use modules in JavaScript. A large module may contain some code which could be very useful to us and we'd have absolutely no idea about it. However, we can still look up that module and use certain functions inside it which could be very useful to us. Common examples of modules are express, body-parser that we typically use every time we're building a backend for our application that help us with creating a server and reading the response's body.

JavaScript Tutorial »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.