I run the test.js file

import { exec, spawn , fork } from 'child_process'; const ls = spawn(`/data/files/test.js` , [] , { detached : true, shell : true }); ls.stdout.on('data', (data) => { console.log(`stdout: ${data.toString()}`); }); ls.stderr.on('data', (data) => { console.log(`stderr: ${data.toString()}`); }); ls.on('exit', (code) => { console.log(`child process exited with code ${code.toString()}`); }); 

But errors are displayed.

 > /data/files/test.js: 2: /data/files/test.js: let: not found > /data/files/test.js: 3: /data/files/test.js: Syntax error: "(" > unexpected 

Contents of the test.js file

 let logStr = ""; var fs = require("fs"); 
  • Well, yes, because you launch it with a shell, not a node - andreymal
  • And how to run the node? - manking
  • 2
    In command specify the path to the node, in args path to the executable file - andreymal
  • 2
    or write #!/usr/bin/env node in the first line of the file and make the file executable - nörbörnën

0