0

I'm relatively new to javascript and I've run into the following issue which has been bugging me for a few hours now. I'm working with the following javascript and html file, but for some reason I can't seem to access the variables in test.js from ui.html

test.js



var ans;
 
function execShellCommand(cmd) {
    const exec = require('child_process').exec;
    return new Promise((resolve, reject) => {
     exec(cmd, (error, stdout, stderr) => {
      if (error) {
       console.warn(error);
      }
      resolve(stdout? stdout : stderr);
     });
    });
   }

 async function myasyncfuntion() {  

   ans = await execShellCommand('sh diffcheck.sh');
   console.log(ans);
   
   }

   
myasyncfuntion();

ui.html

<!DOCTYPE html>
<html lang="en-us">
  <head>
    <meta charset="utf-8" />
    <script type="text/javascript" src="test.js"></script>
    
  </head>
  <script>

     const diffString = ans;
     alert(diffString);

  </script> 
 
</html>

Can anyone explain to me why ans from test.js is not printed in the alert window when ui.html is run? And how can I fix it?

  • for one thing, your function is `async`, so even if your function worked, `ans` would still be undefined - see [this question and the answers](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). But more importantly, your code is using NodeJS features like `require`, and trying to execute shell commands - things which simply will not work when running in a browser. – Robin Zigmond Nov 24 '22 at 20:41
  • The project I'm working on requires that I use the output from a shell command to be printed on a browser. Is it not possible using javascript at all? – newtocoding Nov 24 '22 at 20:49
  • not running in the browser, no. That shell command has to run on some machine, and JS running in the browser runs on the client's machine - but can't execute arbitrary shell commands for very obvious reasons of security (imagine what a malicious website or script could do to an unsuspecting user's machine if that were possible!). – Robin Zigmond Nov 24 '22 at 21:05
  • you could run a shell command with JS (or any other language) on the *server* and return the result of that to the front end to display to the user, if that was thought useful. I really don't know what you're actually trying to do. – Robin Zigmond Nov 24 '22 at 21:05
  • You are most likely getting errors. Could you add those to your question? – Dr. Vortex Nov 24 '22 at 21:19
  • Error: Command failed: sh diffcheck.sh at ChildProcess.exithandler (child_process.js:308:12) at ChildProcess.emit (events.js:314:20) at maybeClose (internal/child_process.js:1022:16) at Socket. (internal/child_process.js:444:11) at Socket.emit (events.js:314:20) at Pipe. (net.js:676:12) { killed: false, code: 1, signal: null, cmd: 'sh diffcheck.sh' } diff --git a/f3.txt b/f4.txt index d90b8f1..f496a53 100644 --- a/f3.txt +++ b/f4.txt @@ -1,3 +1,2 @@ hello world -hello world world -helo hello world +hello hello – newtocoding Nov 24 '22 at 22:33
  • That's the output when I run test.js – newtocoding Nov 24 '22 at 22:34
  • that looks the output when you run it on your machine via the command line with node. Browsers do not know anything about nodejs, and you will get a very different output (full of errors) when it's run in the browser! – Robin Zigmond Nov 24 '22 at 22:43
  • Yes, my browser output is simply an alert window with "undefined" – newtocoding Nov 24 '22 at 22:46

0 Answers0