1

I'm trying to pass variables to a file that I required in node.js so I can use them in that file.

var myname = "Adnan";
var incfile = require('./ex.js');

I'm wishing to be able to use myname variable in the ex.js file.

How am I supposed to do this?

1 Answer 1

4

I'm wishing to be able to use myname variable in the ex.js file.

I understand you want to do something like this so that myname is used by ex.js functions:

var myname = "Adnan";
var incfile = require('./ex.js')(myname);

So your ex.js should export a function:

var mynameInEx;
module.exports = function(myname) {
    mynameInEx = myname; // or whatever you want to do with myname here
    var incfile = 'returnedValue'+myname; // or whatever you want to return
    return incfile;
};
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. This was the answer I was looking for. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.