askmeaquestion1234
askmeaquestion1234

Reputation: 179

How to import export modules return values to other js file

Trying to grasp the export import/export process and carrying over functions and their returns to another file. I came across this great example! But having trouble bringing that over into another file..

For example I get this error when running node testing.js . Which I thought you could pass your parameters over.

ERROR Output

console.log(testClass.authy)(app);
                             ^

ReferenceError: app is not defined

helper.js

module.exports.auth = function (app) {
    console.log("return authy")
    var app = "1";
    return app;
};

testing.js

const testClass = require('../commands/helper.js');
console.log(testClass.auth)(app);

Upvotes: 0

Views: 1468

Answers (1)

Noam Eyal
Noam Eyal

Reputation: 328

First of all, when logging the result of a function to the console, you should use console.log(function()), not console.log(function)().

Second of all, you are passing 'app' in as an argument, which is a value you give to the function, and then redefining it straight away. Your function doesn't need any arguments, it should just be function() { ... }, and then be called as testClass.auth(). Right now, you are trying to pass a variable 'app' into your function, which you haven't defined yet.

So in the end, your code should be:

helper.js

module.exports.auth = function () {
  console.log("return authy")
  var app = "1";
  return app;
};

testing.js

const testClass = require('../commands/helper.js');
console.log(testClass.auth());

The value of 'app' is returned to the console.log function, which then displays it. Hope this helps!

Upvotes: 1

Related Questions