execSync stays put and does not return any value

Keywords´╝Ü javascript node.js express

Question: 

I am working in a CodeCademy-like application, where users type in some code and it runs and the result is then displayed in the console. To do this, I've created an express environment and in the website, users can type their code and then send it throug a POST request.

My problem is with the following code:

const fs = require('fs');
const {execSync} = require('child_process')

function run(code) {
  var path = __dirname + '/../temp/temp.js';
  fs.writeFileSync(path, code);
  console.log('file written');
  let out = execSync('node ' + path); // code stays put
  console.log('file executed') // does not happen
  return out.toString();
}

module.exports = {
  run: run
};

I write the user input in the temp.js file, which then I plan to execute with execSync. The first console.log is executed, the second one is not.

I am calling it from the following route:

router.post('/', (req, res, next) => {
  res.render('index', {
    instructions: reader.readInstructions('lesson1.txt'),
    console: runner.run(req.body.editor) 
  })
})

The weird thing about this problem, is that this code:

const fs = require('fs');
const {execSync} = require('child_process')

String code = 'var a = 0\nvar b = 5\nconsole.log(a*b)';

fs.writeFileSync('../temp/temp.js', 'code');
let out = execSync('node ../temp/temp.js');
console.log(out.toString());

works just as expected (returning the value of the String) if I run it with the command

node runner.js

from the console.

Any help is greatly appreciated :)

Answers: