Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

295
Views
Bash script calls a .js file with a function that should update global variable, but it doesn't

I have a updateGlobalVar.js file which looks as following:

var globalVar = 111;

async function setGlobalVar() {
    globalVar = 222;
}

setGlobalVar();

Also, I have a bash script, which retrieves that globalVar value, and looks as following:

#!/bin/bash

npm run /updateGlobalVar.js
GLOBAL_VAR=$(grep -Po '(?<=^var globalVar = ).*?(?=;)' ./updateGlobalVar.js)

echo $GLOBAL_VAR

The problem here is the bash script prints out initial 111 instead of an updated 222.

The way I understand that flow is:

  • npm run /updateGlobalVar.js runs updateGlobalVar.js (and I am sure it does);
  • function setGlobalVar() is called (and I am sure it is);
  • globalVar should be now updated and printed with a new 222 value. But it doesn't.

Moreover, when I modify my file to "debug" using the console.log() within the updateGlobalVar.js file like:

var globalVar = 111;
console.log(globalVar);

async function setGlobalVar() {
    globalVar = 222;
}

setGlobalVar();
console.log(globalVar);

It returns 111 in both console.log(globalVar) cases, which makes me think that globalVar is never updated.

Looks like I miss something fundamental regarding the work with global variables, still am struggling to figure that out.

May this be somehow related to the asynchronous nature of the setGlobalVar() function?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!