How to include Functions from other files in Node.js ?
Last Updated :
29 Jun, 2020
Code reusability is an important pillar in modern day programming. Code Reuse means the practice of using an existing code for a new function or software. In this article, we would learn how to use functions from other files in Node.js.
This functionality can be easily implemented using the inbuilt export and require functions of Node.js.
Export: The module.exports in Node.js is used to export any literal, function or object as a module. It is used to include JavaScript file into Node.js applications. The module is similar to variable that is used to represent the current module and exports is an object that is exposed as a module.
Require() function: It is an inbuilt function and is the easiest way to include functions that exist in separate files. The basic functionality of require is that it reads a JavaScript file, executes the file, and then proceeds to return the export object.
Let us consider the following basic example:
Filename: cal.js
javascript
function sum(x, y) {
return (x + y);
}
function sub(x, y) {
return (x - y);
}
function mul(x, y) {
return (x * y);
}
module.exports = { add, sub, mul, div };
In the above example, we use the module.exports function so that we can use it in other files. The functions are enclosed within curly brackets( { } ) according to the format to export multiple functions at a time.
Suppose we wanted to use these functions in main.js, then it can be easily done using the following code:
Filename: main.js
javascript
//requiring cal.js file
const cal = require("./cal.js")
//Using the functions from cal.js
const sum = cal.sum(2, 2);
console.log(sum);
const sub = cal.sub(10, 5);
console.log(sub);
const product = cal.mul(2, 3);
console.log(product);
This will import the cal.js file and its functions into the main.js file.
Run
main.js file using the following command:
node main.js
Output:
4
5
6
Similar Reads
How to Dynamically Call Router Function in Node.js ? In Node.js Dynamically calling a router function means that the function is selected at runtime based on the request parameters, instead of being explicitly defined in the code. This can be useful when you want to handle a large number of similar requests without having to define a separate function
4 min read
How to Override Functions of Module in Node.js ? Overriding functions in a Node.js module allows you to alter or extend the behaviour of existing functions without modifying the original module's code. This approach can be useful for customizing library functionality, adding new features, or fixing bugs in third-party modules. Hereâs how you can a
3 min read
How to add new functionalities to a module in Node.js ? Node.js is an open-source and cross-platform runtime environment built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. You need to recollect that NodeJS isnât a framework, and itâs not a programming language. In this article, we will discuss how to add new functi
3 min read
How to Install NPM FS in Node JS ? The file system module allows you to work with the file system on your computer NodeJS includes the fs module, to communicate with file systems. It provides functionality for interacting with the file system, such as reading from and writing to files, creating and removing directories, etc. The File
4 min read
How to Access the File System in Node.js ? In this article, we looked at how to access the file system in NodeJS and how to perform some useful operations on files. Prerequisite: Basic knowledge of ES6Basic knowledge of NodeJS NodeJS is one of the most popular server-side programming frameworks running on the JavaScript V8 engine, which uses
3 min read