Import and Export in Node.js

19 Aug, 2020
Importing and exporting files are important parts of any programming language. Importing functions or modules enhances the reusability of code. When the application grows in size, maintaining a single file with all the functions and logic becomes difficult. It also hinders the process of debugging. Therefore, it is good practice to create separate files for specific functions and later import them as per requirement.
Node.js also allows importing and exporting functions and modules. Functions in one module can be imported and called in other modules saving the effort to copy function definitions into the other files. The module can be edited or debugged separately making it easier to add or remove features.
Steps to include functions from other files:
Creating a Module: Modules are created in Node.js are JavaScript files. Every time a new file with .js extension is created, it becomes a module.Exporting a Module: Filename: func.jsfunction add(x, y) {   return x + y;}  function subtract(x, y) {   return x - y;}  // Adding the code below to allow importing// the functions in other filesmodule.exports = { add }Importing a Module: We need to import the module to use the functions defined in the imported module in another file. The result returned by require() is stored in a variable which is used to invoke the functions using the dot notation.Filename: main.js// Importing the func.js module  // The ./ says that the func module// is in the same directory as // the main.js fileconst f = require('./func');  // Require returns an object with add()// and stores it in the f variable // which is used to invoke the required   const result = f.add(10, 5);  console.log('The result is:', result);Output:The result is: 15
Creating a Module: Modules are created in Node.js are JavaScript files. Every time a new file with .js extension is created, it becomes a module.
Exporting a Module: Filename: func.jsfunction add(x, y) {   return x + y;}  function subtract(x, y) {   return x - y;}  // Adding the code below to allow importing// the functions in other filesmodule.exports = { add }
function add(x, y) {   return x + y;}  function subtract(x, y) {   return x - y;}  // Adding the code below to allow importing// the functions in other filesmodule.exports = { add }
Importing a Module: We need to import the module to use the functions defined in the imported module in another file. The result returned by require() is stored in a variable which is used to invoke the functions using the dot notation.Filename: main.js// Importing the func.js module  // The ./ says that the func module// is in the same directory as // the main.js fileconst f = require('./func');  // Require returns an object with add()// and stores it in the f variable // which is used to invoke the required   const result = f.add(10, 5);  console.log('The result is:', result);Output:The result is: 15
// Importing the func.js module  // The ./ says that the func module// is in the same directory as // the main.js fileconst f = require('./func');  // Require returns an object with add()// and stores it in the f variable // which is used to invoke the required   const result = f.add(10, 5);  console.log('The result is:', result);
Output:
The result is: 15
Importing multiple functions from local file: Filename: func.js
function add(x, y) {  return x + y;}  function subtract(x, y) {  return x - y;}  module.exports = { add, subtract};
Filename: main.js
const f = require('./func');  console.log(f.add(4, 4));console.log(f.subtract(8, 4));
We can also use the destructuring syntax to unpack the properties of the object returned by require() function and store them in respective variables.
const { add, subtract} = require('./func');console.log(add(4, 4)); console.log(subtract(8, 4)); 
Output:
8
4

Other ways to export a module
Defining the functions inside module.exports object.module.exports = {  add: function (x, y) {    return x + y;  },    subtract: function (x, y) {    return x - y;  },};
module.exports = {  add: function (x, y) {    return x + y;  },    subtract: function (x, y) {    return x - y;  },};
Defining each function independently as a method of module.exportsmodule.exports.add = function (x, y) {   return x + y;};  module.exports.subtract = function (x, y) {   return x - y;};
module.exports.add = function (x, y) {   return x + y;};  module.exports.subtract = function (x, y) {   return x - y;};
Importing a module from a directory: Importing lib.js file inside the directory, by prefixing lib.js with the directory name.
const lib = require('./mod/lib');  console.log(lib.add(6, 4));console.log(lib.subtract(12, 4));
There are three types of modules in Node.js
Importing from local module: These modules are created by the user and can be imported as:const var = require('./filename.js'); // OR
const var = require('./path/filename.js');
Importing from core modules: These modules are inbuilt in Node.js and can be imported as:const var = require('fs');Importing from third party modules: These modules are installed using a package manager such as npm. Examples of third party modules are express, mongoose, nodemon, etc. These are imported as:const express = require('express');
Importing from local module: These modules are created by the user and can be imported as:const var = require('./filename.js'); // OR
const var = require('./path/filename.js');

const var = require('./filename.js'); // OR
const var = require('./path/filename.js');

Importing from core modules: These modules are inbuilt in Node.js and can be imported as:const var = require('fs');
const var = require('fs');
Importing from third party modules: These modules are installed using a package manager such as npm. Examples of third party modules are express, mongoose, nodemon, etc. These are imported as:const express = require('express');
const express = require('express');
Thus above are few examples to import and export functions from different files in Node.js .
Node.js-Misc
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.