NodeJS Export and Import Modules

In our previous posts, we have discussed about “How to install Enide Studio 2014 IDE” and also “How to create a Node JS Application”. Before discussing about “How to create new Node JS Modules and How to reuse them in other Node JS Modules”, first of all we need to have some good knowledge about how to export and import a Node JS module. In this post, we are going to discuss the following two important Node JS concepts theoretically.

  • How to export a Node JS Module
  • How to import a Node JS Module

We will use this knowledge especially in the next post, but also in all my coming posts’ examples.

Export a Node JS Module

First and foremost thing we need to understand is why we need to export a Node JS Module? Node JS has provided almost all required modules (We can check this updates on its official website: https://www.npmjs.com/. It is also known as Node JS Module Repository). But in some realtime applications, we may have some application functionality, which is used many places in that application but not available at Node JS Module Repository. In this scenario, to get Reusability benefit, we need to create our own new Node JS module. Just creating new Module is not enough to use it in other modules of the system. We need to export this module so that other modules will reuse it. If you are a UI or Java Developer, then this is familiar to you. If we have any common or reusable component in Java-based application, then we develop it as a separated project, create a Jar file and add it at required project’s classpath. Don’t worry too much about how to create a Node JS Module at this point. We will discuss how to create our own new Node JS Module in a future post. Node JS Platform has provided a technique to export the following things so that other modules can reuse them without redefining.

  • Variable
  • Function
  • Module

To export all these three, we have to use the same technique. Node JS has provided an Object “exports” to do this. Syntax:

exports.[variable/function/module]name = [variable/function/module]name

We will see one by one now with some examples. Here we are discussing them theoretically only. We will take one realtime scenario and implement one Node JS Application in my next post.

How to export a JavaScript Variable

We use Node JS “exports” object to export a Node JS Module’s Variable so that other Modules can reuse it. In realtime, we don’t just export a Variable. However, just for practice purposes and also to make it clear to beginners, we are discussing this separately. Syntax:

exports.variable-name = variable-name

Example: Let us assume that we have created a simple Node JS Project with one JavaScript file: pi.js file with the following content.

var PI = 3.1416
exports.PI = PI;



Here we have exported PI variable as exports.PI with name PI. That means other Node JS project can use this variable very easily just using PI name.

How to export a JavaScript Function

We use Node JS same “exports” object even to export a Node JS Module’s JavaScript function so that other Modules can reuse it. In realtime, we may do this but it is not recommended to use always. Syntax:

exports.function-name = function-name

Example: Let us assume that we have created a simple Node JS Project with one JavaScript file: arthmetic.js file with the following content.

function add(a,b){
  return a + b;
}

function sub(a,b){
  return a - b;
}

function mul(a,b){
  return a * b;
}

function div(a,b){
  return a / b;
}

exports.add = add
exports.sub = sub
exports.mul = mul
exports.div = div



Here we have exported all 4 JavaScript functions separately. That means other Node JS projects can reuse them directly. It is a simple Arithmetic application. However, if we take some realtime Node JS Applications, we can observe plenty of JavaScript files and each file may contain plenty of functions. In this scenario, it is very tedious to export each and every function separately. To resolve this issue, we need to use Exporting a Node JS module technique as described below.

How to export a Node JS Module

We use Node JS same “exports” object even to export a Node JS Module so that other Modules can reuse it. In realtime, it is recommended. Syntax:

exports-module-name = module-name

Example: Let us assume that we have created a simple Node JS Project with one JavaScript file: arthmetic.js file with the following content.

exports.arthmetic = {

  var PI = 3.1416;
  function add(a,b){
    return a + b;
  }

  function sub(a,b){
    return a - b;
  }

  function mul(a,b){
    return a * b;
  }

  function div(a,b){
    return a / b;
  }
}



Here we have exported all 4 JavaScript functions and PI variable with just one exports statement. That means other Node JS projects can reuse all functions and PI very easily.

Import a Node JS Module

If we observe a Node JS Application or Module, it may be dependent on other existing Node JS modules or our own Modules. The major advantage of Modularity is reusability. We don’t need to redevelop the same existing functionality. We can import a Module and reuse that module functionality very easily.

How to import a Node JS Module

We use the same technique to import our modules or existing Node JS Modules. Node JS Platform has provided a function call “require()” to import one module into another. Syntax:

var some-name = require(“module-name”)

Here module-name is our required Node JS module name. some-name is reference name to that module. This require() call imports the specified module and caches it into the application so that we don’t need to import it again and again.

Example:

To import our own Node JS module:

var arthmetic = require("arthmetic");

To import existing Node JS Module:

Import Node JS “express” module:

var arthmetic = require("express");

Import Node JS “mongoose” module:

var mongoose = require("mongoose");

This require() call is similar to “import” statement in Java. We use import statement to import a package, class, interface, etc. into another class or interface. Now we got some knowledge about how to export and import a Node JS module. We will use this knowledge to create our own Node JS Modules in the next post.

Fazit

Jetzt haben Sie ein Verständnis für die verschiedenen Komponenten, die LVM verwaltet, um ein flexibles Speichersystem zu erstellen, und wie Sie Speichergeräte in einer LVM-Konfiguration einsatzbereit machen können. Um mehr über die Arbeit mit LVM zu erfahren, werfen Sie einen Blick in unseren Leitfaden zur Verwendung von LVM mit Ubuntu 18.04.

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: