简体   繁体   中英

Pass variable in required file node.js

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?

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;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM