This page looks best with JavaScript enabled

How to Create a Function and Import It as a Node Module

 · ☕ 1 min read · 👽 john hashim

creating a reusable code is what makes programming more fun and easy .

lets create a funtion and import it in other files so that we can reuse it over and over .

1
2
3
4
5
6
function minus(a, b) {
  if (b === undefined) return -a;
  else return a - b;
}
//make functional reusable global 
exports.minus = minus;

now you can impot this funtion in any file

1
2
3
4
5
6
7
8
//import your funtion
const result = require('<file directory>')

//how to call for the funtion
 result.minus(10, 5);

 //ans : →  5

Share on

john hashim
WRITTEN BY
john hashim
Web Developer