programing

phantomjs“SyntaxError : Parse error”메시지에서 더 많은 정보 얻기

copyandpastes 2021. 1. 19. 08:20
반응형

phantomjs“SyntaxError : Parse error”메시지에서 더 많은 정보 얻기


내가 작성하지 않은 긴 대본이 있습니다. 내가 그것을 실행할 때 나는 얻는다 :

phantomjs file.js
SyntaxError: Parse error

나는 매뉴얼과 --help를 확인했고 내가 생각 해낼 수있는 최선의 방법은 :

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

최소한 줄 번호를 얻는 더 좋은 방법이 있습니까? 또는 전혀 힌트?


node를 사용 하여 파일을 실행하십시오 . 구문 분석 오류가 있으면이를보고합니다.

파일이 유효하면 노드 도 실행을 시도합니다. 스크립트가 노드 환경 에서 사용할 수없는 항목에 의존하는 경우 실패 합니다. 따라서 런타임 오류를 무시해야합니다.

예를 들어 hello-world.js가 주어 지면 :

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

노드로 실행하십시오 .

node hello-world.js

산출:

/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

당신은 file.js잘못된 구문이 포함되어 있습니다. 구문 검사기로 확인해야합니다. 내가 만든 온라인 도구가 가능한 해결책이 될 수 있습니다 . http://esprima.org/demo/validate.html을 확인하십시오 .


PhantomJS에서 추가 정보 얻기

PhantomJS의 다음 버전 (1.9.7 이후에 나오는 것은 1.9.8이 될 것임)은 다음과 같은 오류를 출력합니다.

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

따라서 현재 메시지보다 약간 더 유용합니다.

안타깝게도 PhantomJS에 대한 야간 빌드가 없기 때문에이 시간을 사용하려면 마스터 버전을 직접 컴파일해야합니다.

축소 된 파일 디버깅

축소 된 파일로 작업하는 경우 종종 줄 번호가별로 도움이되지 않으며 축소되지 않은 파일을 디버깅해도 구문 분석 오류가 발생하지 않는 경우가 많습니다.

이를 해결하기 위해 phantomjs가 파일 이름을 제공하면 Esprima의 온라인 데모를 사용 하여 JavaScript 코드의 실제 구문 분석을 얻을 수 있습니다.

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

여기에서 전략적 줄 바꿈을 입력하여 실제 오류를 격리 할 수 ​​있습니다.

Lint 도구는이 사용 사례에 적합하지 않습니다.

jslint 또는 jshint와 같은 린트 도구는 실제 파서보다 더 의견이 많으므로 매우 구체적인 구문 오류를 찾고 있다면 실제 파서를 사용하는 것이 좋습니다. 나는 Lint 도구가 가치가 없다는 것을 의미하는 것이 아니라 이러한 유형의 문제를 해결하는 데 도움이되지 않을뿐입니다.


jslint 또는 jshint와 같은 구문 린터를 사용할 수도 있습니다.


사용자 지정 축소 jquery SyntaxError: Parse error를 시도하는 동안 받았습니다 require.

해결책은 의 맨 아래에 빈 줄추가하는 것 입니다 jquery.min.js.

바라건대 이것은 누군가를 도울 것입니다. PhantomJS 1.9.7 사용.


구문 분석 오류가 발견되지 않으면 Phantom을 정상적으로 실행하는 간단한 셸 스크립트를 사용합니다. 그렇지 않으면 오류가 표시됩니다. 노드를 통해 스크립트를 실행하고 SyntaxError출력에서 확인한 다음 오류가 있으면 노드를 터미널로 다시 실행합니다. 모든 명령 줄 인수를 Phantom에 전달합니다.

이 방법의 실패 모드는 노드 파서가 Phantom 파서와 크게 다른 경우입니다. 내 것은 문제가되지 않을만큼 충분히 가깝습니다.

용법: ./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

나는 같은 문제가 있었지만 어떤 해결책도 나를 위해 일하지 않았습니다.

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.

ReferenceURL : https://stackoverflow.com/questions/14905664/getting-more-information-from-phantomjs-syntaxerror-parse-error-message

반응형