6

I am running on Linux Mint 16 'petra' 64 bit, and I am trying to use 'phantomjs' for the first time, together with node js.

I have installed phantomjs globally:

sudo npm install -g phantomjs

... and confirmed that it is running (I get the phantomjs prompt by running "phantomjs" in terminal)

and I have installed the node 'phantom' module within my node project:

npm install phantom

So far so good.

However, within my application code, as soon as it attempts to execute this line:

var phantom = require('phantom');

... the program crashes with the following trace:

Listening on port 3000
about to instantiate phantom module...
module.js:333
    throw err;
          ^
Error: Cannot find module 'weak'
    at Function.Module._resolveFilename (module.js:331:15)
    at Function.Module._load (module.js:273:25)
    at Module.require (module.js:357:17)
    at require (module.js:373:17)
    at new D (/home/joe/Documents/My Stuff/Programming/Angular.js Projects/NodeJS Messing/FreeAgentScraper/node_modules/phantom/node_modules/dnode/index.js:28:20)
    at module.exports (/home/joe/Documents/My Stuff/Programming/Angular.js Projects/NodeJS Messing/FreeAgentScraper/node_modules/phantom/node_modules/dnode/index.js:8:12)
    at /home/joe/Documents/My Stuff/Programming/Angular.js Projects/NodeJS Messing/FreeAgentScraper/node_modules/phantom/phantom.js:135:13
    at Server.handler (/home/joe/Documents/My Stuff/Programming/Angular.js Projects/NodeJS Messing/FreeAgentScraper/node_modules/phantom/node_modules/shoe/index.js:22:9)
    at Server.EventEmitter.emit (events.js:104:17)
    at App.emit (/home/joe/Documents/My Stuff/Programming/Angular.js Projects/NodeJS Messing/FreeAgentScraper/node_modules/phantom/node_modules/shoe/node_modules/sockjs/lib/sockjs.js:182:27)
    at Session.emit_open (/home/joe/Documents/My Stuff/Programming/Angular.js Projects/NodeJS Messing/FreeAgentScraper/node_modules/phantom/node_modules/shoe/node_modules/sockjs/lib/transport.js:107:23)

I can confirm that indeed there is no 'weak.js' anywhere in the project.

I have run 'npm install' to ensure that all dependencies have been installed.

Google search hasn't revealed anything worthwhile. Can anyone offer any advice?

user2209634
  • 529
  • 7
  • 27

3 Answers3

19

I had the same issue using node.js and phantom in a windows machine, what I found that you need some special treatment on the windows enviroment. the npm document is here.

var phantom = require('phantom');
phantom.create(function(ph) {
    return ph.createPage(function(page) {
      return page.open("http://www.google.com", function(status) {
         console.log("opened google? ", status);
         return page.evaluate((function() {
            return document.title;
      }), function(result) {
        console.log('Page title is ' + result);
        return ph.exit();
      });
    });
  });
}, {
    dnodeOpts: {weak: false}
});

notice phantom.create(function, options, callback), and here options uses dnodeOpts: {weak: false}

user2500299
  • 476
  • 1
  • 4
  • 9
3

Have you installed build-essential in your system? Because weak is a module that needs to be compiled locally. I had got it working in an Ubuntu system a week ago by installing build-essential (sudo apt-get install build-essential), but now when I try in Linux Mint, it gives me the same error you are getting. During npm install -g phantom, it warns that weak 0.3.1 could not be installed.

Have you found any solution yet?

EDIT

And, by the way the key difference between the system where I was successful and the current one is that, the first one was 32 bit, the latter where it is failing is 64 bit. Does the module "weak" have any problems that prevents it from installing on a 64 bit box?

WORKING SOLUTION

I got this resolved by following these steps:

  1. sudo add-apt-repository ppa:fkrull/deadsnakes
  2. sudo apt-get update
  3. sudo apt-get install python2.6
  4. sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.6 20
  5. python --version should give you 2.6.x. Weak module does not get installed when Python version is higher than 2.6.x.

After this make sure that your node and npm are latest versions. I have node v0.10.28 and npm v1.4.9.

If you have phantom module already installed, remove it by running npm uninstall phantom. Then run npm cache clean.

Now, install weak separately: npm install weak. If that goes through, install phantom by running npm install phantom.

That should resolve the issue.

rkrishnan
  • 776
  • 1
  • 9
  • 21
  • Current version of node: v0.11.14-pre Current version of npm: 1.4.9 Did all of the above. Fails part way through 'npm install weak' > weak@0.3.1 install /home/joe/Documents/My Stuff/Programming/Angular.js Projects/NodeJS Messing/FreeAgentScraper/node_modules/weak > node-gyp rebuild gyp ERR! configure error gyp ERR! stack Error: "pre" versions of node cannot be installed, use the --nodedir flag instead gyp ERR! stack at install (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/install.js:66:16) – user2209634 May 22 '14 at 10:49
  • Hmmm, I never faced that issue. I found couple of stackoverflow questions related to your issue [here](http://stackoverflow.com/questions/12947145/error-pre-versions-of-node-cannot-be-installed-use-the-nodedir-flag-instea) and [here](http://stackoverflow.com/questions/18777882/node-gyp-error-while-doing-npm-install). Check them out. I guess using a stable version of node.js instead of `v0.11.14-pre` will resolve it. Or maybe use the `--nodedir` flag. – rkrishnan May 26 '14 at 07:15
0

I had the same issue and this code fixed my issue. Phantom version = 1.9.8 Phantom npm module version = 0.8.4

`

var phantom=require('phantom');
phantom.create(function( ph ){  
    },{
        dnodeOpts: {
            weak: false
        },
        parameters:{
            'web-security': 'no'
        }
});

`

Prasad19sara
  • 332
  • 2
  • 10