I'm trying to run phantomjs in a node child process. Phantomjs complains it can not find module 'webpage'. Running the script from the cli works fine:
phantomjs script.js
I stripped the problem down to these two files:
main.js:
var script = require('./script');
var cp = require('child_process');
cp.exec('/usr/bin/phantomjs script.js');
script.js
var page = require('webpage').create();
page.open("http://www.google.com", function(status) {
console.log("opened google? ", status);
var title = page.evaluate(function(s) {
return s;
}, 'Hello');
console.log(title);
phantom.exit();
});
Running the following command fails:
node main.js
with error:
module.js:340
throw err;
^
Error: Cannot find module 'webpage'
I've tried to specify the working directory in cp.exec as an option without effect. Is there a way to set the node search path for modules in a global context so it can be found in a new node process? Or am I doing something else wrongly?
PhantomJS is not a node.js module and cannot be directly used from node. I think you understand this, because you try to invoke it via child_process.
The problem is the line var script = require('./script');. As I understand, it is the phantomjs script. Since node.js doesn't have a webpage module, but phantomjs does the require fails and everything with it. Simply remove the line. It doesn't seem like you use it anyway.
Related
I'm running electron and nodejs on Windows 10
How can I find a path to an executable?
one way would be to run a cli command that will use something like
where myfile.exe
is there a node-ish way?
Use which module:
var which = require('which')
// async usage
which('node', function (er, resolvedPath) {
// er is returned if no "node" is found on the PATH
// if it is found, then the absolute path to the exec is returned
}).
I am running forllowing test.js file using
>node test.js
test.js is simple code
var fs = require("fs");
fs.readFile('2.rtf', function (err, data) {
if (err) return console.error(err);
console.log(data.toString());
});
console.log("Program Ended");
It shows error
so naturally I remove the extension and ran following command which worked fine.
node test
I am running windows 7 32 bit. How to stop WSH while using node commnad?
The problem seems to be that you have a file named node.js in the same directory and due to the way Windows resolves commands, it's trying to execute the file node.js (using the default handler for that file type, which is typically JScript) instead of node the node.js executable. Rename the node.js file to something else and it should work.
So I have a file running in node which runs a local copy of PhantomJS as below shows:
phantom.casperPath = 'node_modules/casperjs';
phantom.injectJs('node_modules/casperjs/bin/bootstrap.js');
var casper = require('casper').create({
viewportSize: config.viewportSize
});
casper.test.begin('Runing tests here', 5, function suite(test) {
// Tests here
});
Without the casper.test.begin() my tests function fine. I have the correct version 1.1.0 which can use this test suite but I get the following error in my console:
CasperError: casper.test property is only available using the `casperjs test` command
The CasperJS docs mentions this too: http://docs.casperjs.org/en/latest/testing.html. My question is how do I run this Casper under this command in the above code so I can use these tests?
Thanks!
CasperError: casper.test property is only available using the casperjs test command
problem solved.
You have to include this line at the top of your script in your xyz.js, so that the .test property becomes true;
phantom.casperTest = true;
Then you should have no problem Launching from the terminal:
casperjs xyz.js
you can also call casperjs test xyz.js
For more info, check the doco here : http://docs.casperjs.org/en/latest/testing.html
I'm attempting to use PhantomJS, and I've installed it via NPM.
I can't seem to run any of the of the examples, in fact I can't even run:
var page = require('webpage').create();
I get the error:
Error: Cannot find module 'webpage'
Is there anything i'm missing? I'm using a few other modules that I've installed via NPM in the same directory with no issues
PhantomJS is not for Node.js. You are likely running the examples through node binary.
Read the Getting Started documentation carefully and you'll see that every single PhantomJS example need to be invoked like:
phantomjs hello.js
Note that there is a bridge between Node.js and PhantomJS. In that case, you need to follow the given examples for that particular bridge (there are a few different ones).
You can use something like this:
var page = new WebPage();
Example of code :
var page = new WebPage();
page.open('http://example.com', function(status) {
console.log("Status: " + status);
if(status === "success") {
page.render('example.png');
}
phantom.exit();
});
I've successfully got Phantomjs to work on Heroku but now I'm hitting issues with the phantomjs-node interface for node.js (see https://github.com/sgentle/phantomjs-node).
When I'm attempting to initialize Phantom I'm seeing a 10-15 second delay and then:
> phantom stdout: ReferenceError: Can't find variable: socket
phantom stdout: phantomjs://webpage.evaluate():1
phantomjs://webpage.evaluate():1
phantomjs://webpage.evaluate():1
You can reproduce the problem with the following steps or by pulling down my test app at https://github.com/matellis/phantom-test
git init phantom-test
cd phantom-test
heroku apps:create
# create node app as per Heroku instructions here https://devcenter.heroku.com/articles/nodejs
# copy bin and lib folders from http://phantomjs.googlecode.com/files/phantomjs-1.6.1-linux-x86_64-dynamic.tar.bz2 into root of your new project
# if you don't do this step you'll get an error "phantom stderr: execvp(): No such file or directory"
git add .
git commit -m "init"
git push heroku
Test your app has come up, the third to last line will tell you the URL, it should read like:
http://fathomless-ravine-5563.herokuapp.com deployed to Heroku
If successful you should see Hello World! in your browser.
Now from the same folder as your Heroku application run:
heroku run node
At the node prompt try the following:
phantom = require('phantom');
x = phantom.create();
Wait 10-15 seconds and you should see the error. Nothing works from this point on.
This should output file foo.png:
x = phantom.create(function(ph){ph.createPage(function(page){ page.open('http://bbcnews.com', function(status){ page.render('foo.png', function(result) {ph.exit()}); }); }); });
To validate Phantomjs is working fine on Heroku, try the following using my test project:
>heroku run bash
Running `bash` attached to terminal... up, run.1
~ $ phantomjs test.js http://bbcnews.com foo.png
~ $ ls *.png
foo.png
I cannot reproduce any of these problems locally but there are other issues reported where folks may have hit this issue locally.
The problem seems to originate in shim.js line 1637:
s.on('request', function(req) {
var evil;
evil = "function(){socket.emit('message', " + (JSON.stringify(JSON.stringify(req))) + " + '\\n');}";
return controlPage.evaluate(evil);
});
I've tried variations of versions of node, phantom, etc. with no luck.
I've also tried a custom buildpack that sets the DYLD variable, see http://github.com/tecnh/heroku-buildpack-nodejs with no luck either.
Anyone who has got Phantom + Node playing together nicely on Heroku please let me know. There are several references to this on Stackoverflow but nobody is saying "I got it to work, here's how".
I've never used the phantomjs node module, but I do have an app running both node and phantomjs on Heroku.
You need to use custom buildpacks in order to get this to work. My .buildpacks file looks like
http://github.com/heroku/heroku-buildpack-nodejs.git
http://github.com/stomita/heroku-buildpack-phantomjs.git
You should then be able to run phantomjs scripts in a child process:
var script = app.get('root') + '/scripts/rasterize.js' //the phantomjs script to run
, bin = app.get('phantom') //normally this would just be the string "phantomjs"
, spawn = require('child_process').spawn;
// set up args to the phantom cli
// (run the phantomjs command in your terminal to see options/format)
var args = [];
// ...
var phntm = spawn(bin, args);
phntm.stdout.on('data', function (data) { /* do something */ });
phntm.stderr.on('data', function (data) { /* do something */ });
phntm.on('exit', function (code) { /* handle exit */ });
Heroku does not support WebSockets. With Socket.io it has a workaround. Not sure about dnode, which phantomjs-node uses.
I had a problem with WebSockets on Heroku too and I switched to Nodejitsu, which solved it for me.