Introduction
Generally, while programming, when the same task has to be performed more than once, we prefer to make a function for the same. Right?
Let’s say, there’s is this large project wherein there are different sub-tasks having features of their own. So, you make those sub-tasks and save them as a different file to use it as and when required. In JavaScript, these sub-tasks are called modules, and since the whole project depends on them, these modules are said to be dependencies.
Modules enable the developer to write re-usable code.
Imports, exports, and module.exports help us reuse the modules in the entire project.
Let’s learn about imports, exports, and module.exports in depth.
Before starting with the explanation of these, let’s first make a few files that can be referred to throughout the article. This will make it easier to understand.
Files we will be using throughout the blog:
We have a file named file1.js. Be careful with the extension: it should be .js as it is a JavaScript file. Also, assume that both these files are in the same directory. It is essential that both of them must be in the same directory because the structuring of files in the further code throughout the article has been done according to it.
These files are basically the modules. Each file is considered to be a module.
file1.js looks like this:
console.log(“Hi! This is file1.js)”);
function user_name(name)
{
console.log(name +”! You are a Ninja”);
}
function user_gender(gender)
{
console.log(“You are a ”+gender);
}
For file2.js, it looks like this:
console.log(“Hi! This is file2.js”);
user_name(“Coder”);
Since we have the files now. Let’s move forward and explore imports first.
Also Read, Javascript hasOwnProperty
Imports
‘Import’- the term in English implies inflow of some commodity. Like, the import of sugar in the country means bringing in the entity, sugar.
The import keyword in JavaScript is used to get the code from another module.
A very important thing to note here is that import is an ES6 module specification.
Usage
The syntax for the same is as follows:
import {name of functions, classes} from ‘path of the file’;
Here, if more than one function or class is imported, write the names of all of them separated by commas. ‘path of the file’ includes the name of the file in case both the files/modules are in the same directory or in case both lie in different directories, then the name of the directory should also be there.
For example, the file2.js does not have functions of user_name() and user_gender(). But we have already defined these functions in file1.js. So, in order to use those functions from file1.js, we need to import them from there to whatever file we want to use them. In our case, it is file2.js.
Something like this:
import {user_name,user_gender} from ‘./file1.js’;
After adding the above line at the start of the code of file2.js, it is essential to add it at the beginning of the code.
import {user_name,user_gender} from ‘./file1’;
console.log(“Hi! This is file2.js”);
user_name(“Coder”);
Now, if you add the following code to file2.js:
user_name(“Coder”);
It will print the following in the console window:
Coder! You are a ninja.
Similarly the function user_gender() function can be used in file2.js:
user_gender(“female”) ;
The output will be:
You are a female
So, you see how by just defining the function only once, you can use them in different files by importing them.
You can import only one of the functions if you want by mentioning only that name inside the curly braces as follows:
import {user_gender} from ‘./file1.js’;
But then, you will only be able to use that function in the file where you have imported it.
If there are many functions, and you want to import all of them but feel too lazy to write all the names, you can import all of them using asterisk *.
Something like this:
import * from ‘./file1.js’;
Now you can use all the functions of file1.js.
You can also create the alias for the same like this:
import * as allfunctions from ‘./file1.js’;
Here, an object named ‘allfunctions’ is the object and user_name() and user_gender() are its functions. They are accessed similar to how an object's member function would be done.
allfunctions.user_name(“Coder”);
Aliases can be used for each function or class as well as follows:
import { user_name as un,user_gender as ug} from ‘./file1’;
The functions should be given meaningful names(although we have given quite vague names for understanding’s sake).
Properties
1. By default, import modules are in strict-mode.
2. import, when used with curly braces {}, is a statement while when used with brackets() like import() is a function.
3. import statement can only be used in embedded script with script=”module”, while dynamic function import() does not require such type in script.