VinoPravin
VinoPravin

Reputation: 987

Export and Use same function in node js

I have exported a function to set random color, which returns me a random color for every call.

like,

exports.getRandomColor = function(index) { //return random color }

If I put function like this I can be able to get access to that function from another file. But not inside the same file. (i.e) This getRandomColor is not get accessed in the same file,

It works good if I use

function getRandomColor(index) { //return random color }

The problem is I cannot able to access this on another file. Suggestions are welcome.

Thanks in advance

Upvotes: 1

Views: 81

Answers (1)

The Reason
The Reason

Reputation: 7983

So you can have the next in your file

// define named function
function getRandomColor(index) {
    return random color
}
// assign it to the exports, like below
exports.getRandomColor = getRandomColor

Then you can reuse this function whenever you want.

Upvotes: 4

Related Questions