0

First of all, yes I know this question has been asked before, but I still cannot figure out how to make it work. I believe the problem is, I am running files individually through node.js on my Mac terminal, sorta like applications.

Here is the deal. I have one file, bitt1.js, that has var mid = 293.03;.

In my other file, otherFile.js, I have an if, else statement, depending on the variable mid (which is in bitt1.js):

if (mid <= 290) { 
    trade = true; 
} else { 
    trade = false; }

The issue is, in terminal, I run first bitt1.js, then after I run otherFile.js. This makes it so I can't receive the mid variable from bitt1.js and it comes up as undefined.

How can I solve this issue? I've only found things used within html or etc, where the variables are always "available".

I'm new to JS and this whole thing so some of the stuff I said may be incorrect... and I could have also just been being dumb and the answer is obvious, but please help me out... I've thought about creating a JSON file and writing/reading data from it using the two other files, but I feel there's a better way...

Thanks!

4
  • Why not implement the naiive solution first? Writing and reading from a JSON file is a perfectly valid solution (until you have multiple programs attempting to edit it at the same time). Any reason why you don't want to implement it this way? Commented Nov 4, 2017 at 6:09
  • I could I was just struggling with the concept a little cause I'm new to this, I wanted to see if there was another fix before going into that, which y'all helped me with! I'll work on JSON implementation as well though, for good measure. Thanks again! @JonathanBrooks Commented Nov 4, 2017 at 7:52
  • If I were to write information to a json, would I do something like fs.appendFile, and create an array? what would you recommend? Commented Nov 4, 2017 at 7:57
  • fs.appendFile should work fine! Just make sure you convert your JSON object to a string using JSON.stringify first Commented Nov 4, 2017 at 8:50

2 Answers 2

1

Developer NodeJS's code works if you don't want to modify the value of the variable - if you just want to share the initial value of the variable, it works perfectly.

But if you intend to mutate the value of mid during runtime execution of bitt1.js and want to use that value, perhaps you can use a Unix pipe to plug its value into the stdin of bitt1.js.

E.g.

// bitt1.js
var mid = 299;
console.log("<mid>%d</mid>", mid); // this is piped to stdin

// otherFile.js
var stdin = process.openStdin();
var data = "";
stdin.on('data', function(chunk) {
  data += chunk;
});
stdin.on('end', function() {
  data.match(/<mid>([\s\S]+)<\/mid>/i);
  var mid = +data.match[1];
  console.log(mid);
});

Then running: node bitt1.js | node otherFile.js

Would print 299 from within otherFile.js.

This is a rough solution though: it should require some undefined checking on the match expression, and of course piping doesn't allow you to print anything directly to console in the bitt1.js file - you'd have to reprint everything in otherFile.js, which leads to duplicate code.

But it could be a solution that works for you, all depends on your requirements! Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Clever solution.
1

node.js allows imports and exports.

Say bitt1.js has:

var mid = 299

console.log(mid)

// Here is where you export the desired value
//export default mid
module.exports.mid = mid

Then, in your otherFile.js

// you import the value from bitt1.js
var mid = require('./bitt1')

console.log(mid) //Outputs 299

That's it.

Edit: updated answer

2 Comments

I have to be honest after coding a bit more I use this a ton haha. Thanks so much!
Don't be afraid to accept it as an answer my friend. Cheers!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.