3

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 }

HendrikEng
  • 654
  • 1
  • 10
  • 32
  • 2
    Globals are evil. Do not use globals if you can help it. – Quentin Feb 13 '18 at 16:45
  • 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 Feb 13 '18 at 16:46
  • 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 Feb 13 '18 at 16:47
  • 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 Feb 13 '18 at 16:49
  • So why not do `module.exports = { env: env, func: }` – Frank Modica Feb 13 '18 at 16:51
  • Which variable do you need to make available where? Your code does lots of passing things around that I cannot quite track. – Bergi Feb 13 '18 at 16:54
  • "*since i do need the return for something else already*" - what? why can't you just return both things? – Bergi Feb 13 '18 at 16:54
  • 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 Feb 13 '18 at 16:58

2 Answers2

3

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);
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

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);
pouyada
  • 341
  • 3
  • 15