i am a bit lost:

I am trying to make a variable available in another function, since i do need the return for something else already. i thought i could just make the variable global available and import it in FileB, whats wrong and might there be a better way? thanks a lot

FileA:

const variableToPass = null;

module.exports = (env) => {
    variableToPass = env;

    const varA = require(`./FileB.js`); 
    const varB = doThis(varA, …doSomething());
    return varB;
};

module.exports = variableToPass;

FileB:

const variableToPass = require(‚./.FileA‘);

console.log(variableToPass);

it does log : { [Function] variableToPass: null }

2 upvote
  flag
Globals are evil. Do not use globals if you can help it. – Quentin
upvote
  flag
yes thats what i thought :-) but i have no clue how to pass the var to the file via require or similar inside the module.export in the env function – HendrikEng
upvote
  flag
Your import is now a function with an env property, so it looks like it works, although I'm not sure you wanted to attach a property to a function. – Frank Modica
upvote
  flag
yes the property is param i use in the function, but i would like to make the property available in fileB as well , wich i require in the function – HendrikEng
upvote
  flag
So why not do module.exports = { env: env, func: <your function> } – Frank Modica
upvote
  flag
Which variable do you need to make available where? Your code does lots of passing things around that I cannot quite track. – Bergi
upvote
  flag
"since i do need the return for something else already" - what? why can't you just return both things? – Bergi
upvote
  flag
i tried to clean it up, its a webpack config with different merges, as soon as ireturn something else it stops working. sorry for the confusion – HendrikEng

2 Answers 11

up vote 1 down vote accepted

You have several problems here.

  1. const env defines a constant, so you can't change it
  2. (env) => { masks it with a local variable of the same name, so you don't even try to change it
  3. You never call that function anyway
  4. module.exports.env = env; copies the value of env at the time the statement is evaluated, so even if you did change env, it wouldn't change the value of the exported property

Just export a sensible data structure which contains everything you need.

// called.js

var env = null;

function stuff() {
    env = 1;
    return 2;
}

function get_side_effect() {
    return env;
}

module.exports = {
    stuff: stuff,
    get_side_effect: get_side_effect   
};

// caller.js

const caller = require("caller");

var stuff = called.stuff();
var side_effect = called.get_side_effect();
console.log(stuff, side_effect);

or return a sensible data structure from your function:

// called.js

var env = null;

function stuff() {
    env = 1;
    return {
        value: 2,
        env: env
    };
}


module.exports = {
    stuff: stuff
};

// caller.js

const called = require("called");

var stuff = called.stuff();
console.log(stuff.value, stuff.env);

Even if this is not the best practice you can try to use getter and setter functions and export your whole class. in Addition the env must be a let not a const.

setEnv(){
  env = val;
}

getEnv(val){
   return env;
}

in FileB:

console.log(fileA.getEnv);

Not the answer you're looking for? Browse other questions tagged or ask your own question.