16

I was recently going to test out running phantomJS from python as a commandline argument, I haven't got round to it yet but have seen examples. Because PhantomJS is run from the command line this seems to be possible. The result that PhantomJS would spit out would go straight into a variable.

Before I go down that path, making this work in node.js would actually be more useful for me and it got me thinking, can i just use to node to run PhantomJS as a program gets run from the commandline and store the data result that PhantomJS would normally spit out into a variable?

I would rather not use phantomjs-node because it seems to be using too many tricks.

The reason for all of this is to be able to run PhantomJS at the same time as another action the program takes and use the resulting data its recorded for some other stuff.

Simply put, you can run system command line stuff in python, can I do the same in node.js?

Cheers :)

Edit: I understand that node and phantom use different js environments, that's cool because I just want to run phantom as its own process and catch all that output data into a node.js variable (the data will be a array of a pair, string and floating point.) I don't want to 'drive' with phantom, I will craft the loaded javascript files todo what I want. All I want is phantom output. :)

Joseph
  • 3,899
  • 10
  • 33
  • 52
  • 1
    Most people seem to be using [`child_process.spawn`](http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options) to achieve this. `child_process` is a stable standard node.js module now, maybe it wasn't at the time you asked - node.js moves quite rapidly. – hippietrail Dec 26 '12 at 02:08

3 Answers3

19

From NPM: https://npmjs.org/package/phantomjs

var path = require('path')
var childProcess = require('child_process')
var phantomjs = require('phantomjs')
var binPath = phantomjs.path

var childArgs = [
  path.join(__dirname, 'phantomjs-script.js'),
  'some other argument (passed to phantomjs script)'
]

childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
  // handle results
})
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Donald Derek
  • 2,529
  • 2
  • 23
  • 17
14

I suppose you can make a simple script for Node.js to run; in that script phantomjs script will be run as a child process. You can see the working example (and links for some documentation) in this answer. I suppose this discussion might be helpful for you as well.

Community
  • 1
  • 1
raina77ow
  • 103,633
  • 15
  • 192
  • 229
  • I'm going over the two links and it looks like they are directly addressing my question. Cheers :) – Joseph Jun 29 '12 at 11:57
  • For those too lazy to click through, one of the links mentions https://github.com/sgentle/phantomjs-node which is of interest. – MarkHu Dec 19 '12 at 01:46
5

As an alternative to Donald Derek's answer, you can use the spawn function. It will allow you to read the child process's output as soon as it's produced rather than the output being buffered and returned to you all at once.

You can read more about it here.

An example from the documentation.

var spawn = require('child_process').spawn,
    ls    = spawn('ls', ['-lh', '/usr']);

ls.stdout.on('data', function (data) {
  console.log('stdout: ' + data);
});

ls.stderr.on('data', function (data) {
  console.log('stderr: ' + data);
});

ls.on('close', function (code) {
  console.log('child process exited with code ' + code);
});
zbr
  • 6,860
  • 4
  • 31
  • 43
  • 1
    how would you run lets say a local ExpressJS server AND spawn a sub process for phantom, Casper, etc.? Like if I go to the command prompt and run my server.js which has my express server and starts it, then how do you invoke the spawning of the child process after you start that initial express server? – PositiveGuy Feb 23 '16 at 21:16