Reputation: 3034
I do have PhantomJS installed, but I get the error (you don't have 'phantomjs' installed) when I run my Node.js code:
var modules = '/home/engine/node_modules/';
var path = require('path');
var childProcess = require('child_process');
var phantom = require(modules+'phantom');
var binPath = phantom.path;
phantom.create(function(browser){ // Error happens here I think because the module is found
// browser.createPage(function (page){});
});
If in console.log binPath I get undefined.
But in PuTTY, if I:
cd ~/phantomjs/
[root@engine phantomjs]# bin/phantomjs
phantomjs>
Have I installed it in the wrong place?
Upvotes: 4
Views: 3658
Reputation: 485
I was also getting this error at my macOS, so this command did the trick.
brew install phantomjs
edit: phantomjs was moved to Cask. So in 2019 run:
brew cask install phantomjs
Upvotes: 4
Reputation: 3233
The accepted answer in this case was not really a solution. The error message You don't have 'phantomjs' installed
is an internal error from the phantomjs-node module. I ran into this error myself, and I managed to fix it like this:
var phantom = require('phantom');
var options = {
path: '/usr/local/bin/'
};
phantom.create(function (ph) {
ph.createPage(function (page) {
page.open("http://www.google.com", function (status) {
console.log("opened google? ", status);
page.evaluate(function () { return document.title; }, function (result) {
console.log('Page title is ' + result);
ph.exit();
});
});
});
}, options);
Notice the options
being passed to the phantom.create()
method. The path
option should be the full path to the directory that contains your phantomjs binary.
Upvotes: 1
Reputation: 448
But if you are on Windows, you can try some thing like this:
var phantom = require('phantom');
phantom.create(function (ph) {
ph.createPage(function (page) {
page.open("http://www.google.com", function (status) {
console.log("opened google? ", status);
page.evaluate(function () { return document.title; }, function (result) {
console.log('Page title is ' + result);
ph.exit();
});
});
});
}, {
dnodeOpts: {
weak: false
}
});
Upvotes: 0
Reputation: 6206
You need to load your global PhantomJS module and not the local.
Loading the local module prevents you application from locating the runnable bin:
var phantom = require('phantom');
Plus, adding utnas comment:
Remove var modules = '/home/engine/node_modules/';. It's not useful. Node.js knows where to find modules.
Mixing both parts of the answer to a logical rule, Node.js will always first load the module from the global installed modules if it exists. You force it to load the local one and that prevents it from finding the bin.
Upvotes: 3