1

I'm currently struggling with using Phantom.js with a Meteor app of mine. I have it installed on my local machine (Ubuntu 14.04), it's added to my path (I can run it from my terminal), I also ran and installed the smart wrapper for Phantomjs: mrt add phantomjs.

I can see that in my .meteor > local > build > programs > server > npm directory there is a phantomjs directory.

My question is, how do I actually use Phantom? I'm attempting to scrape from the server side of things. I've tried the following things (using coffeescript): phantom = Npm.require "phantomjs" phantom = Npm.require "phantom" phantom = Meteor.require "phantomjs" phantom = Meteor.require "phantom"

(I've also tried using capital "P's")

All attempts in this way yield: Error: Cannot find module 'phantomjs'

Any clarification would be greatly appreciated!

Alex_Faber
  • 117
  • 1
  • 8
  • Are you installing it to use with the spiderable package? If so then it just needs to be on the PATH so it can be used to create pages for bots (more is explained in http://www.manuel-schoebel.com/blog/meteor-and-seo). – ElDog May 22 '14 at 16:28
  • I'n not actually. I'll just be using it to do some scraping. – Alex_Faber May 22 '14 at 16:35

2 Answers2

4

[EDIT] now meteor is supporting npm packages out of the box: https://guide.meteor.com/using-npm-packages.html#installing-npm


Here is the procedure for Meteor > 1.0.0

Add the npm package

meteor add meteorhacks:npm

Run meteor to let the npm package to pre-initialise

meteor

A file packages.json has been created at the root. Edit it to:

{
  "phantomjs": "1.9.13"
}

To use phantom into your server side code:

var phantomJS = Meteor.npmRequire("phantomjs");

Bonus: an example of usage (thanks Ben Green), put anywhere in your code:

if (Meteor.isServer) {
    Meteor.startup(function () {
        var phantomjs = Meteor.npmRequire('phantomjs');

        var spawn = Meteor.npmRequire('child_process').spawn;
        Meteor.methods({
            runTest: function (options) {
                command = spawn(phantomjs.path, ['assets/app/phantomDriver.js']);
                command.stdout.on('data', function (data) {
                    console.log('stdout: ' + data);
                });
                command.stderr.on('data', function (data) {
                    console.log('stderr: ' + data);
                });
                command.on('exit', function (code) {
                    console.log('child process exited with code ' + code);
                });
            }
        });

        Meteor.call("runTest");// run the test as soon as meteor server starts
    });
}

Create the phantomjs script file ./private/phantomDriver.js and edit it to

var page = require('webpage').create();
page.open('http://github.com/', function (){
    console.log('Page Loaded');
    page.render('github.png');
    phantom.exit();
});
Flavien Volken
  • 19,196
  • 12
  • 100
  • 133
  • I think you should create the phantomjs script file inside `/private/phantomDriver.js` instead. – Maximus S Apr 21 '15 at 13:02
  • @MaximusS, assuming I just started the app from my meteor directory, your path is exactly the same as the one I wrote. The leading dot stands for the current working directory which in this case is the meteor app folder. (Not to confuse with a leading dot before a file or directory name which purpose is to hide this last one) – Flavien Volken Apr 21 '15 at 13:11
  • ah I see. Do you have experience with `spiderable` package by any chance? – Maximus S Apr 21 '15 at 13:14
  • another quick question Flavien. Do you have to add phantom to PATH manually even if you install it this way? – Maximus S Apr 21 '15 at 13:43
  • @MaximusS I am not an expert but I guess meteorhacks:npm will install the package in the same place Meteor.npmRequire('phantomjs'); can retrieve it. If you need your website to be spiderable take a look [here](https://gentlenode.com/journal/meteor-12-the-complete-guide-to-seo/20). If you need to install everything on a production server (including PhantomJS) go for [MUP](https://github.com/arunoda/meteor-up) and an Ubuntu distribution. Or if you feel adventurous, [Docker](https://www.docker.com) + MUP – Flavien Volken Apr 21 '15 at 14:28
  • "Error: Can't find npm module 'webpage'". what do I do? – DeadlyBacon Oct 24 '16 at 15:20
3

The phantomjs wrapper in atmosphere doesn't look like it produces anything that works.

But you can easily add npm packages useing the npm meteorite package

First add the npm package to your project

mrt add npm

Then add the required phantomjs version to the packages.json file

{
     "phantomjs": "1.9.7-6"
}

Then use the following code to require the phantomjs npm module:

var phantomjs = Meteor.require('phantomjs');
Marco de Jongh
  • 5,270
  • 3
  • 17
  • 29
  • That definitely gets's me own the right track! I used that method before and was pointing to `phantom` instead of `phantomjs`. Thank you very much. – Alex_Faber May 22 '14 at 16:50
  • You're welcome, btw I read in your other comment that you're gonna doe some scraping. Be sure to Check out out cheerio if your scraping static sites that dont load extra stuff with ajax. https://www.npmjs.org/package/cheerio – Marco de Jongh May 22 '14 at 20:16
  • ya for scrapping phantom makes it easy but also its much heavier. cheerio and request make the best light combo. – Mustafa Nov 05 '14 at 12:07