0

how can I use one method into entire project is it possible? If it's possible please help me with detail.

Thanks Advance

ravi kumar
  • 21
  • 3

1 Answers1

0

As an example its your log.js file.You can use module.exports to export a function like this

module.exports.log = function (msg) { 
    console.log(msg);
};

Now if you want to import it in ,suppose app.js file.Just do something like this.

var msg = require('./Log.js');// the path is important here 

msg.log('Hello World');

In above example you are exporting log function ,import it with the way I told and you can use it anywhere you want (after requiring).

Better have a look at the docs too.

You can also use ES6 export and import,something like below

export function log(){ } // exports named function log
import { log } from '..filepath/filename'

MDN docs ,describe it elaborately.

Shubham Dixit
  • 9,242
  • 4
  • 27
  • 46