55

I have a long script that was not written by me. When i run it I get:

phantomjs file.js
SyntaxError: Parse error

i checked out the manual and --help, and the best i could came up with was:

phantomjs --debug=yes file.js
(irrelevant debug statement from CookieJar)
SyntaxError: Parse error

Is there any better way to get at least a line number? or any hint at all?

2
  • FYI: If you use big arrow syntax for lambdas (i.e. => ) your script will fail and you will get no parser error for feedback.
    – bnieland
    Commented Jun 7, 2019 at 14:47
  • this question is getting votes/answers/comments and it is from a time when lambda syntax was not even being dreamed on in javascript yet! ...people just use firefox headless! phantomjs is abandoned (and chrome/ium is dead to me)
    – gcb
    Commented Jul 15, 2019 at 1:39

7 Answers 7

82

Run the file with node. If there is a parse error it will report it.

If the file is valid, then node will also try to run it, which will fail if your script depends on something not available in your node environment. So you'll have to ignore any runtime errors.

For example, given hello-world.js:

// Say Hello World twice
for (var i=0; i<2; i++) {
  console.log("Hello World") );
}

Run it with node:

node hello-world.js

Output:

/home/someone/somewhere/hello-world.js:3
  console.log("Hello World") );
                             ^
SyntaxError: Unexpected token )
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3
4
  • This is a quick and clever way to run local syntax checking on js files. I find this much quicker than the online tools, plus it is un-opinionated. the downside is needing node, but I suspect most phantomjs devs will have node installed.
    – danmux
    Commented Sep 11, 2013 at 1:05
  • What happens if node encounters some window global? Or if phantom, which runs.. chromium? is a different version to Node? It's ok for some basic cases I guess
    – nanobar
    Commented Jan 25, 2016 at 18:27
  • my recipe on all makefiles: find src/js/ -name \*js -exec node \{\} \; 2>&1 | grep -B 5 SyntaxError || exit 0 && exit 1; it will fail Make rule if there any syntax error and show you the line where it happens.
    – gcb
    Commented Feb 11, 2016 at 21:43
  • 1
    This answer is no longer valid. If the file is written in ES2015 it will run fine in Node.js 6+, but fail in Phantom 2.
    – Pavlo
    Commented Aug 9, 2016 at 12:31
12

Your file.js contains an invalid syntax. You should check it with a syntax validator. An online tool which I have created can be one possible solution, check out http://esprima.org/demo/validate.html.

1
  • Accepting this one (it was the first to suggest checking syntax elsewhere) as it is obvious that phantomjs has no way to hint at the syntax error by itself.
    – gcb
    Commented Oct 25, 2013 at 21:54
10

Getting More Information From PhantomJS

The next version of PhantomJS (presumably it will be 1.9.8, whatever comes after 1.9.7) will output errors like this:

SyntaxError: Parse error
http://localhost:9000/scripts/49e8b4f4.vendor.js:8

So that's slightly more useful than the current message.

Unfortunately there are no nightly builds for PhantomJS, so at this time you will need to compile your own version of master if you want to try this.

Debugging Minified files

If you are working with a minified file, often the line number won't be very helpful, and often debugging the un-minified file won't give you the parse error.

To solve this, once you get phantomjs to give you the name of the file, you can use the online demo of Esprima to get an actual parse of your JavaScript code:

http://esprima.org/demo/parse.html

From there, you can enter strategic line breaks to isolate the actual error.

Lint Tools are Sub-optimal for this use-case

A lint tool like jslint or jshint are more opinionated than a real parser, so if you are looking for a very specific syntax error, I would recommend using a real parser, since it will check only for code validity, and not opinionated styling guidelines. I'm not implying lint tools don't have value, just that they won't be as helpful for solving this type of problem.

1
  • 2
    1.9.8 still has only syntax error :: and in new 2.0 script hang randomly, so just Ctrl + Z ;)
    – user956584
    Commented May 9, 2015 at 15:35
2

You can also use syntax linters like jslint or jshint

2
  • 1
    A linter does more than syntax validation. In many cases, it is slightly more opinionated. Commented Feb 16, 2013 at 16:02
  • i tried jslint and yes, it was too opinionated and exited the code after some 100 complains about my choice of style (i.e. function( var ){ ) or whitespace usage, or something else as frivolous... by the time i found all the settings to make it sane, i had already found the syntax error. :/ will try jshint the next time though!
    – gcb
    Commented Feb 20, 2013 at 18:29
0

I received SyntaxError: Parse error while trying to require a custom minified jquery.

I found the solution was to add a blank line to the bottom of jquery.min.js.

Hopefully this helps someone. Using PhantomJS 1.9.7.

1
  • Why the down-vote? It's a bug and it was helpful to someone else already.
    – jchook
    Commented Apr 5, 2018 at 0:21
0

I use a simple shell script that will run Phantom normally if no parse errors are found, otherwise it will display the errors. It runs the script through node, checks for SyntaxError in the output and then re-runs node to the terminal if there are errors. It passes all the command line arguments along to Phantom.

Failure mode for this method is if your node parser is significantly different from your Phantom parser. Mine are close enough that it's never been a problem.

usage: ./debug.sh --myArg1 val1 --myArg2 val2

debug.sh:

ERROR=$(node myscript.js 2>&1 >/dev/null |  grep "SyntaxError")

if [ "$ERROR" = "" ]; then
  echo "No parse errors, running script..."
  # run the script with Phantom and pass all the args to it
  phantomjs accession.js $*
else
  echo "*********************************************************"
  echo "********* There are parse errors in the script: *********"
  echo "*********************************************************"
  # show the errors by running again but not redirecting
  node accession.js
fi
0

I did have the same issue, but none of the solutions worked for me.

I figured that the issue was probably an unsupported syntax by PhantomJS's webkit, since my page was working correctly in current browsers' versions.

What I did was finding out which webkit version PhantomJS was using, for 1.9.* it's here, 34.534.

Now we have to find the Chromium version using the same webkit, or close to it, in here (for Mac).

Ended up going with the Mac OSX 267668 and installed it.

Loaded the same URL as PhantomJS, and here it was, a real Uncaught SyntaxError: Unexpected token with a full stack trace.

Hope that helps.

Not the answer you're looking for? Browse other questions tagged or ask your own question.