How to import and export using module object in NodeJS

NodeJs is an open-source JavaScript run time environment using the chrome V8 engine to execute JavaScript. It helps us run JavaScript code outside of the browser environment enabling it to function as a backend language.

NodeJs helps us write reusable code in the form of modules in Js files. In order to access these modules, we can export and import them into the relevant files.

In order to execute the code in this article, you must have NodeJs installed on your device.

ECMAScript 6 (ES6) is the latest revision of javascript used today. This revision offers two ways to export JavaScript modules:

  • Named Export
  • Default Export

Named Export

Individual objects, functions, classes, and variables can be exported from modules using the 'export' keyword. Lets us look at some example code.

Code for Export

Let’s look at an example of exporting individual components in a module.
Create a Js file named 'export.js' and copy the following code into this file using an IDE (Integrated Development Environment) like VsCode.

export const Array = [1,2,3,4];
const animals = ['Panda', 'Bear', 'Eagle'];

// Not available directly outside the module
export function myLogger() {
 console.log(animals);
}

export const add = (a,b) =>{
       return a + b;
}

export const subtract = (a,b) => {
       return a - b;
} 


Explanation

  • Line#1: We use 'export' on the 'Array' of numbers. The 'export' keyword helps export the array from the file.  
  • Line#2: We define an array of strings named 'animals'. This array is not accessible outside of this file.
  • Line#5-7: The 'myLogger()' function is defined which prints the contents of the 'animals' array using the 'console.log()' method.
  • Line#9-11: The 'add' arrow function is passed parameters 'a' and 'b' and returns their sum.
  • Line#13-15: The 'subtract' arrow function is passed parameters 'a' and 'b' and returns their difference.

Named Import

Create an 'import.js' file and copy the following code into this file. Enter 'node import.js' in the terminal to execute the code.

import * as module from "./export.js"

console.log(module.Array);
console.log(module.add(1, 2));
console.log(module.subtract(1, 2));
module.myLogger();

Explanation

  • Line#1: The 'import' keyword is used followed by '*' indicating to import every variable, function, class, and object with the 'export' keyword. The 'as' keyword is followed by the 'module' object used to access the imported module members.
  • Line#3: The contents of 'Array' are printed in the terminal.
  • Line#4: The sum of '1' and '2' is returned from the 'add()' function and printed in the terminal.
  • Line#5: The difference between '1' and '2' is returned from the 'subtract()' function and printed in the terminal.
  • Line#6: The 'myLogger()' function is called using the module object.

Output

import and export using module object
import and export using module object

Default Export and Import

The 'default' keyword can be used only once inside a Js file to export an object, variable, class, or function. 

In the 'export.js' file remove the 'export' keyword and add the following line at the end.

export default {Array, add, subtract, myLogger};

In the 'import.js' file replace the first line with the following line of code:

import module from "./export.js"

You may also like:

Stay in the Loop

Get the daily email from Algoideas that makes reading the news actually enjoyable. Join our mailing list to stay in the loop to stay informed, for free.

Latest stories

- Advertisement -

You might also like...