Create a 'library' to be used globally, list of methods

You must Login before you can answer or comment on any questions.

I am looking to create a JS file that can be access from all pages of my app. This JS file is to contain common functions (check if a file exists, etc).

I was thinking of creating a file (let's call it customLib.js for now) and then 'require' it in my code. I then want to be able to call functions in the following way:

fileHandler = require('customLib.js');
 
var fileExists = fileHandler.fileExists('blahblah.png');
Could someone outline the basic syntax required in the customLib.js file in order to achieve the above? Or am I way off on how I would be doing this?

P.S I'm sure there are already functions to check if a file exists, this is just an example.

Thanks

1 Answer

Accepted Answer

var mytools = require('customLib');  // without .js !
mytools.myfirstfunction(options);
mytools.mysecondfunction(options);
 
// and in customLib.js:
 
exports.myfirstfunction = function(options) {
};
exports.mysecondfunction = function(options) {
};

Your Answer

Think you can help? Login to answer this question!