Say I have a string defined like so:const cmd = ` #!/usr/bin/env python print('hello from python') ` How can I execute this string in Node? I know I can save it as a temporary file, then execute that file, something like this:const fs = require('fs') const {spawnSync} = require('child_process') fs.writeFileSync('./script.py', cmd) const res = spawnSync('python', ['./script.py']) console.log(res.stdout.toString()) // "hello from python" However, I would prefer not to save the cmd variable as a temporary file, and execute it directly. Is this possible? Thanks in advance!
Submitted July 20, 2020 at 03:38AM by edwild22
No comments:
Post a Comment