0

I am trying to do one of the node-horseman examples, but I am finding with a problem. The example I am trying to follow is this:

var Horseman = require('node-horseman');
var horseman = new Horseman();

var numLinks = horseman
  .open('http://www.google.com')
  .type('input[name="q"]', 'github')
  .click("button:contains('Google Search')")
  .waitForNextPage()
  .count("li.g");

console.log("Number of links: " + numLinks);

horseman.close();

And the errors it throws when I make phantomjs example.js are these:

Error: Cannot find module 'http'

  phantomjs://bootstrap.js:299 in require
  phantomjs://bootstrap.js:263 in require
  :3
Error: Cannot find module 'tty'

  phantomjs://bootstrap.js:299 in require
  phantomjs://bootstrap.js:263 in require
  :6
TypeError: Object is not a constructor (evaluating 'require('debug')('horseman')')

  :5
TypeError: Object is not a constructor (evaluating 'new Horseman()')

  phant.js:2 in global code

I try to install http locally using npm install http, but after this, there is only a package.json on example/node_modules/http, and if I use npm install in this location, it throws three warnings:

  • it is too the name of a core module
  • no description
  • no repository field

About tty, making a local installation it throws a 404 error.

I try this solution (include npm folder on the path) Nodejs Cannot find module but it didn´t work.

Any suggestion??

Thanks.


EDIT NOT SOLUTION

I reinstall node (now my version is node 0.12.3, npm 2.9.1, and phantomjs 1.9.8), when I try this simple example from the nodejs web:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

And if I run "node example.js" it works, but if I do "phantomjs example.js" the problem persists "Cannot find module http".

I tried install phantomjs via npm ("npm install -g phantomjs") and via downloading the zip on their web, unzipping and adding to the PATH the route to unzipped folder.

One more data (maybe could be a help) my SO is Windows 8.1.

RE-EDIT

I am watching that on the folder where I have installed node, the only folder on node_modules is npm, is it right?? And on C:\Users\Eloy\AppData\Roaming I have two npm folders, one of them is npm-cache and the other one simply npm. The node_modules of this last one doesn´t contain the http module, the npm-cache has a lot of modules and http incluiding... is it important??

Thanks.

Community
  • 1
  • 1
Eloy Fernández Franco
  • 1,350
  • 1
  • 24
  • 47
  • The `http` module is part of the standard node.js installation. This sounds like you don't have node properly installed such that a running instance of node can't find the built-in modules. – jfriend00 May 19 '15 at 23:50
  • Thanks @friend00 but... I think to remember that I ran an express app... and it needs the http module, no?? However, tomorrow i'll uninstall and install again... Is it the correct way, or there is something better?? – Eloy Fernández Franco May 19 '15 at 23:57
  • 1
    You can write yourself a very short test app that does `var http = require('http');` and then uses that module to just see if it works. There are different node.js installation issues on different operating systems (e.g. Windows very different than Linux). – jfriend00 May 19 '15 at 23:58
  • Did you follow these instructions exactly: https://github.com/johntitus/node-horseman#installation? – jfriend00 May 20 '15 at 00:03
  • This might be a version problem. Which versions of PhantomJS, Node, NPM, Horseman do you use? – Artjom B. May 20 '15 at 07:37
  • Hello. @jfriend00, I try "var Horseman = require('http');" and work fine... it makes nothing, but it doesn´t throw any error or warning. – Eloy Fernández Franco May 20 '15 at 07:54
  • @Artjom_B about the version node is 0.10.36, npm is 1.4.28, phantom is 2.0.0 and horseman I install with npm yesterday, making: "npm install node-horseman" on the folder where I am doing the test, I think it is the last version. – Eloy Fernández Franco May 20 '15 at 07:54
  • @jfrined00 And about the installation, node works fine, so I think node is good installed, I try some examples of phantomjs web and they worked fine, I think it is good installed. About horseman, I created a folder from where I execute "phantomjs test_file.js", and inside this folder I made "npm install node-horseman" a new folder called node_modules was created with the module and all its content (dependencies, examples, lib, files, test...). – Eloy Fernández Franco May 20 '15 at 08:03
  • Have you tried installing phantomjs globally (with the `-g` flag)? – slebetman May 20 '15 at 08:37
  • @slebetman uppss!! What I have done to install phantomjs was download the zip from here http://phantomjs.org/download.html, unzip it on a folder and add it to the PATH. How I say, phantomjs seems that works, becouse I tried some samples and they worked fine. – Eloy Fernández Franco May 20 '15 at 08:42
  • Ah. I installed it via npm in the end. What OS are you working on? I don't believe phantomjs offers pre-built linux binaries on their page. – slebetman May 20 '15 at 08:51
  • @slebetman No... I don´t use linux, I am using windows. And try what you say, I remove the phantomjs folder on the PATH, and use npm install -g phantomjs but it doesn´t work... I am going to try removing node and reinstalling it... – Eloy Fernández Franco May 20 '15 at 09:22

1 Answers1

0

+1 your observation in first EDIT i.e scripts run like $ phantomjs can't access global modules via require(), it can access local modules though. Not sure if this is a documented shortcoming of phantomjs

redzedi
  • 1,957
  • 21
  • 31