0

Hey I've got a Nodejs file running on Heroku and I want to call various functions in an external file, but I'm not sure how to do it.

My external tool.js file looks like this:

var Tool = {}; //tool namespace
Tool.doSomething =  function(a,b){}
Tool.doSomethingElse = function(a,b){}

How do I export this functionality to available in my nodejs file?

Presumably in my nodejs file I have something like:

import nodeTool = require('./tool.js');

Then I'd like to call it like:

var n = nodeTool.doSomething(a,b);

Any help is appreciate. Thank you.

1 Answers1

1

tool.js

exports.doSomething = function(a,b){}
exports.doSomethingElse = function(a,b){}

node file:

var nodeTool = require('./tool.js')
var n = nodeTool.doSomething(a,b)
idbehold
  • 16,833
  • 5
  • 47
  • 74
  • Thanks for the quick reply, but what about the namespace? If I change Tool.doSomething to exports.doSomething I think bad things will happen. – Adrian Woods Mar 09 '17 at 21:48
  • This is an explanation for this approach: https://www.sitepoint.com/understanding-module-exports-exports-node-js/ – Danny Narváez Mar 09 '17 at 23:04