Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7660105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:25:15+00:00 2026-05-31T13:25:15+00:00

I have a PhantomJS/CasperJS script which I’m running from within a node.js script using

  • 0

I have a PhantomJS/CasperJS script which I’m running from within a node.js script using process.spawn(). Since CasperJS doesn’t support require()ing modules, I’m trying to print commands from CasperJS to stdout and then read them in from my node.js script using spawn.stdout.on('data', function(data) {}); in order to do things like add objects to redis/mongoose (convoluted, yes, but seems more straightforward than setting up a web service for this…) The CasperJS script executes a series of commands and creates, say, 20 screenshots which need to be added to my database.

However, I can’t figure out how to break the data variable (a Buffer?) into lines… I’ve tried converting it to a string and then doing a replace, I’ve tried doing spawn.stdout.setEncoding('utf8'); but nothing seems to work…

Here is what I have right now

var spawn = require('child_process').spawn;

var bin = "casperjs"
//googlelinks.js is the example given at http://casperjs.org/#quickstart
var args = ['scripts/googlelinks.js'];
var cspr = spawn(bin, args);

//cspr.stdout.setEncoding('utf8');
cspr.stdout.on('data', function (data) {
    var buff = new Buffer(data);
    console.log("foo: " + buff.toString('utf8'));
});

cspr.stderr.on('data', function (data) {
    data += '';
    console.log(data.replace("\n", "\nstderr: "));
});

cspr.on('exit', function (code) {
    console.log('child process exited with code ' + code);
    process.exit(code);
});

https://gist.github.com/2131204

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:25:16+00:00Added an answer on May 31, 2026 at 1:25 pm

    Try this:

    cspr.stdout.setEncoding('utf8');
    cspr.stdout.on('data', function(data) {
      var str = data.toString(), lines = str.split(/(\r?\n)/g);
      for (var i=0; i<lines.length; i++) {
        // Process the line, noting it might be incomplete.
      }
    });
    

    Note that the “data” event might not necessarily break evenly between lines of output, so a single line might span multiple data events.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few pure-JavaScript, client-side tests using PhantomJS. These I'd like to integrate
I trying to run my jamsine specs from command line using PhantomJs.exe. Here is
Have a group of related projects running in SQL Server 2005 for which I
I have a PhantomJS script that loads a local HTML file, injects some javascript
I would like to use CasperJS in node.js. I have referred to the following
Have someone tried out DeCAL in Delphi 2009? I'm thinking about upgrading from 2007,
have not tested on windows. but in ubuntu when u disconnect from the network,
Have you ever had to justify the choice over using .NET instead of Java
I have an issue in flex which is causing a bit of a headache!
I'm using a rake script to tag the AssemblyInfo.cs files in my .NET projects,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.