Installing / Using Phantom.js with Meteor
I am currently struggling using Phantom.js with my Meteor app. I installed it on your local machine (Ubuntu 14.04), it is added to my path (I can run it from my terminal), I also run and install the smart shell for Phantomjs: mrt add phantomjs
.
I can see that .meteor > local > build > programs > server > npm
there is a directory in my directory phantomjs
.
My question is, how do I actually use Phantom? I am trying to clean up 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 also tried to use capital "P")
All attempts thus give: Error: Cannot find module 'phantomjs'
Any clarification would be greatly appreciated!
source to share
[EDIT] Meteor now supports 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 npm package
meteor add meteorhacks:npm
Run meteor to pre-initialize the npm package
meteor
Json file is created at the root. Edit it:
{
"phantomjs": "1.9.13"
}
To use phantom in your server side code:
var phantomJS = Meteor.npmRequire("phantomjs");
Bonus: usage example (thanks Ben Green), put somewhere 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 a phantomjs script file ./private/phantomDriver.js
and edit it
var page = require('webpage').create();
page.open('http://github.com/', function (){
console.log('Page Loaded');
page.render('github.png');
phantom.exit();
});
source to share
The phantomjs wrapper in atmosphere doesn't seem like it produces anything that works.
But you can add npm packages easily using meteorite npm package
First add the npm package to your project
mrt add npm
Then add the required phantomjs version to your packages.json file
{
"phantomjs": "1.9.7-6"
}
Then use the following code to require the phantomjs npm module:
var phantomjs = Meteor.require('phantomjs');
source to share