Using require with casperjs returns "Can't find module" error - require

I'm following the documentation on writing a module with casperjs, but I'm getting an error.
The code, straight from the docs:
// my module, stored in universe.js
// patching phantomjs' require()
var require = patchRequire(require);
// now you're ready to go
var utils = require('utils');
var magic = 42;
exports.answer = function() {
return utils.format("it's %d", magic);
};
And the calling script:
var universe = require("./universe");
var casper = require("casper").create();
console.log(universe.answer());
casper.test.begin("Home Page", 1, function suite(test) {
});
But I get the following error:
casperjs test testStage.js
Test file: testStage.js
CasperError: Can't find module ./universe
/usr/local/Cellar/casperjs/1/libexec/bin/bootstrap.js:214 in patchedRequire
/Users/smosk/Google Drive/source/nest/testStage.js:1
FAIL CasperError: Can't find module ./universe
# type: error
# file: testStage.js
# subject: false
# error: "CasperError: Can't find module ./universe"
# stack: in patchedRequire() in /usr/local/Cellar/casperjs/1/libexec/bin/bootstrap.js:214
in anonymous() in testStage.js:1
FAIL 1 test executed in 0.031s, 0 passed, 1 failed, 0 dubious, 0 skipped.
Details for the 1 failed test:
In testStage.js
Untitled suite in testStage.js
error: CasperError: Can't find module ./universe
Also tried defining casper first:
var casper = require("casper").create();
var universe = require("./universe");
But that just resulted in
Test file: testStage.js
CasperError: Can't find module ./universe
/usr/local/Cellar/casperjs/1/libexec/bin/bootstrap.js:214 in patchedRequire
/Users/smosk/Google Drive/source/nest/testStage.js:2

You should try to upgrade to the lastest master build of CasperJS as this pull request that I made should fix the problem that you're having.
However, if you don't want to upgrade, you could also try running CasperJS with the following:
casperjs test ./testStage.js

for others who might be an idiot like me, you may want to make sure you are using casperjs in your cmd and not phantomjs

Had a similar problem. Mine seems to have been caused by the merge of https://github.com/n1k0/casperjs/issues/587 . Going back to 1d1e83ba3c9241623 solved my problems

I just spent the last 2 hours trying to debug this issue:
C:\casperjs>casperjs
CasperError: Can't find module cli
C:\casperjs\bin\bootstrap.js:262 in patchedRequire
C:\casperjs\bin\bootstrap.js:375
C:\casperjs\bin\bootstrap.js:395
I was using PhantomJS 1.9.0 but I found I had to upgrade to 1.9.2 and it started working immediately. I hope this is helpful to someone else.

Related

Module not found: Error: Can't resolve 'fs' when trying to use a NodeJS library

I initiated a basic ReactJS app using npx create-react-app, then I ejected using npm run eject. Now when I am trying to import the Casual library by import casual from 'casual';, I get the following error:
Compiled with problems:
ERROR in ./node_modules/casual/src/casual.js 3:13-37
Module not found: Error: Can't resolve 'fs'
in '/home/me/project/node_modules/casual/src'
And the code around line number 3 in casual.js looks like this:
var helpers = require('./helpers');
var exists = require('fs').existsSync;
var safe_require = function(filename) {
if (exists(filename + '.js')) {
return require(filename);
}
return {};
};
...
I found answers to similar questions. Those were mainly Node or Angular related. I also tried answers suggesting some changes in webpack config, but no luck.
The reason is Casual doesn't work on the front end. It runs on Node.js only.
You need to install maybe a new package to make things work.
Fs is unavailable on the browser so it won't work. Instead, you should use casual-browserify, it will work on browsers.

How to update express/lib/application.js to match different app.js file?

MEAN noob here. I was following this tutorial, but npm start gives me the following error:
Error: Cannot find module './view'...
at Object.<anonymous> (C:\root\node_modules\express\lib\application.js:22:12)
This is what application.js says at that location:
var query = require('./middleware/query');
var debug = require('debug')('express:application');
var View = require('./view'); /////// line 22
I have a lib/middleware but not a lib/view, nor lib/util...
My theory is perhaps I screwed the express installation up: It's possible I didn't have the correct app.js in place when I installed it. Does app.js affect express's installation?
Is there a way to rebuild express or express/lib without uninstalling & reinstalling everything?
Would this even fix my problem?

'Cannot find module' error using karma-browserify on Windows

I'm attempting to set up unit testing on an Angular/Browserify project using Karma, Karma-Jasmine, and Karma-Browserify. I'm on a Windows machine, for reference. karma-cli is on my global npm path, and karma, karma-jasmine, karma-browserify, and browserify are all local npm installs, using -D.
I'm trying to pull in a single spec file, which looks like:
var PhoneListCtrl = require('../../../public/js/app/controllers/phone-list');
describe('PhoneListCtrl', function() {
var scope,
ctrl;
beforeEach(function() {
scope = {};
ctrl = new PhoneListCtrl(scope);
});
it('should create "phones" model with 3 phones', function() {
expect(scope).not.toBe(undefined);
});
});
And I get the following error every time:
Uncaught Error: Cannot find module 'Cc/gGH'
I get this exact same error after cloning the following repos, installing karma and all plugins, and attempting to run their example test suites:
https://github.com/xdissent/karma-browserify
https://github.com/waye929/angular-browserify
What on earth am I doing wrong? The test spec module is found correctly, and karma seems to be finding all necessary plugins/preprocessors, but it appears that karma-browserify is tripping on the require statement in a spec every time, for reasons I cannot fathom.
I've uninstalled and reinstalled karma and all related plugins numerous times now, to no avail.
I managed to find a solution. The issue was caused by karma-browserify's own module name hashing function, which is incompatible with newer versions of browserify. There's a fork that deals with it by using browserify's hashing function:
https://github.com/voidlock/karma-browserify/commit/3afe3b7485f2e4723bba5ad1c5a730d560b8c234
There's a pull request pending but in the meantime you can use the fork by placing
"karma-browserify": "https://github.com/voidlock/karma-browserify/tarball/use-browserify-hash-function"
in your package.json (dev)dependencies section.

Running 'casperjs test' in phantom

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

Having trouble running Phantomjs + node on Heroku

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.

Resources