3

I have a long CasperJS script. When I run it I get:

phantomjs file.js    
SyntaxError: Parse error

Is there a way to get some more information about the error. At least a line number? or any hint at all?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Anuj Gupta
  • 6,328
  • 7
  • 36
  • 55
  • Possible duplicate of [getting more information from phantomjs "SyntaxError: Parse error" message](http://stackoverflow.com/questions/14905664/getting-more-information-from-phantomjs-syntaxerror-parse-error-message) – bob esponja Apr 22 '16 at 10:31

3 Answers3

5

Try run the file.js with node, so for your example:

node file.js
Tony
  • 1,405
  • 3
  • 20
  • 31
1

It's not possible to determine this in PhantomJS itself. The documentation on phantom.onError says:

This is the closest it gets to having a global error handler in PhantomJS

And this doesn't catch the syntax error. If you try to run it with the --debug=true option, you will see a lot of debug messages, but the final error has still the same amount of information.

Another thing that I tried was to use a second PhantomJS script which reads the original script and tries to eval it. The phantom.onError event is triggered in this case, but the trace argument is empty.

The good thing is that PhantomJS/CasperJS scripts are just JavaScript, so you can paste them to http://jslint.com/ or run a dedicated jslinter on them to see where the problem lies. There are some options that you have to mark on the site or otherwise you will get a lot of errors:

  • add phantom to the global variables box,
  • enable node.js mode and
  • tolerate "everything" (or those things that you actually want to tolerate)
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
0

I spent a whole 8hrs on this to find a trick for this problem. The trick is to run "phantomjs" and type 'require "path_to_js_file"'. I used 2.1.1 version of phantomjs. Likely 2.2 also works.

Then there will be a stack trace that shows which line is the offender. You won't see this in testem output.

In my case, if you define a property twice for an object, it will work for chrome, firefox etc, but not phantomjs. Lint might help but there are >5K lint errors for the project I work on and it is practically impossible to see what's wrong. Also the particular problem is likely hidden under the same bucket of "javascript strict mode violation". Nodejs didn't complain this either.

azuruce
  • 11
  • 1