getting more information from phantomjs “SyntaxError: Parse error” message

前端 未结 7 2214
感动是毒
感动是毒 2020-12-03 04:38

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 a

7条回答
  •  旧巷少年郎
    2020-12-03 04:42

    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.

提交回复
热议问题