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 reference to that library. Now if there is area function defined in the library, then use it as lib.area().
Expample : Write two different programs and then see how to use functions defined in the library (Module) in given program. 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
Importing Module Example
For importing any module, use a function called ‘Require’ which takes in the module name and if its user defined module then its relative path as argument and returns its reference.
Area of the rectangle is 50 square unit Perimeter of the rectangle is 30 unit
Note: To run the script first make both files in the same folder and then run script.js using NodeJs interpreter in terminal.