There is a node file

var spawn = require('child_process').spawn; var phantom = spawn('phantomjs', [ 'snapshot.js', 'http://localhost:3000/#/index/', 'index']); phantom.on('exit', function(code, signal) { if (code !== 0) { return cb(new Error('Exit code is not 0: ' + code)); } cb(); }); 

And there is a snapshot.js file that runs through the file above.

 var page = require('webpage').create(); var system = require('system'); var fs = require('fs'); var url = system.args[1]; var name = system.args[2]; page.open(url, function () { fs.write('snapshot/'+name+'.html', page.frameContent, 'w'); phantom.exit(); }); 

Both files are in the same directory. If you run the phantome.js file separately, then everything works. But starting with a child process I get an error:

 events.js:85 throw er; // Unhandled 'error' event ^ Error: spawn prg ENOENT at exports._errnoException (util.js:746:11) at Process.ChildProcess._handle.onexit (child_process.js:1053:32) at child_process.js:1144:20 at process._tickCallback (node.js:355:11) at Function.Module.runMain (module.js:503:11) at startup (node.js:129:16) at node.js:814:3 

    1 answer 1

    child_process.spawn(command[, args][, options]) - used to invoke an arbitrary command. ENOENT thrown out because you simply do not have phantomjs commands. Accordingly, there is no file to be called for execution (i.e., you need to check that phantomjs is installed).

    If the phantomjs command is there, you need to check that the script for it is in the path of 'snapshot.js' , since The question indicates the path 'phantome.js' .


    Also try passing spawn 3m with the argument {stdio:'inherit'} (redirecting the output of the child process to the parent). And add after calling spawn

     phantom.on('error', function(err){ console.error('parentError:', err); }); 

    Adding the above code, if there is an error in the spawn call, will explicitly let you know that the error is in the call.


    Total :

    The problem exists in the windows environment, a bug since '13. It occurs due to the impossibility of finding the phantomjs command by the system. To solve, you need to specify the full path to the phantomjs .exe file:

    • If installed globally, then (for win8): C:/Users/ %USER% /AppData/Roaming/npm/node_modules/phantomjs/lib/phantom/phantom‌​js.exe

    • Optimal from the point of view of usability is the option of locally installing phantjmjs into a project and using it through %Project_Root%/node_modules/phantomjs/lib/phantom/phantomjs.exe (note that in a multiple installation, more hard disk space will be used)

    • The phantomjs command phantomjs fine if you run it from the command line. I checked that phantomjs is installed because I run the snapshot.js file with the phantomjs snapshot.js command phantomjs snapshot.js and it works. The script is and it works (it was described in the file name) - drm
    • Do the snapshots of the pages add up next to the snapshot.js file? - NumminorihSF
    • No, to the snapshot folder, which lies next to the file snapshot.js - drm
    • Well, I meant it, try to add the code that I added above. accomplish your goal - NumminorihSF
    • received message: parentError: { [Error: spawn phantomjs ENOENT] code: 'ENOENT', errno: 'ENOENT', syscall: 'spawn phantomjs', path: 'phantomjs' } - drm