Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing file and line of javascript syntax error in Safari

For the following error:

SyntaxError: Unexpected token '='. Expected an opening '(' before a method's parameter list.

enter image description here

Safari is not showing a proper stacktrace with file and line. Note there is no inline javascript code in the html: it is all in .js files. How can I get a better handle on where this error is occurring?

like image 996
WestCoastProjects Avatar asked Jul 15 '20 02:07

WestCoastProjects


People also ask

What is a JavaScript syntax error?

The Javascript SyntaxError occurs when trying to interpret code that is not syntactically valid. It is thrown when the Javascript engine comes across tokens or token order that does not conform to Javascript syntax when parsing code.

What is reference error and syntax error in JavaScript?

ReferenceError: Raised when an invalid reference is used. SyntaxError: Raised when a syntax error occurs while parsing JavaScript code. TypeError: Raised when the type of a variable is not as expected. strong text URIError: Raised when the encodeURI() or decodeURI() functions are used in an incorrect manner.

When the browser's JavaScript engine encounters an error it will throw an?

The SyntaxError object represents an error when trying to interpret syntactically invalid code. It is thrown when the JavaScript engine encounters tokens or token order that does not conform to the syntax of the language when parsing code.


1 Answers

Per this answer that links to this post

override your window.onerror like so

window.onerror = function(message, url, linenumber) {
    alert('JavaScript error: ' + message + ' on line ' + linenumber + ' for ' + url);
}

If you're working on a production app you really should use babel, which will solve issues like this for you. If it's not solving these issues make sure to update your packages / npm etc etc.

To get a bit more technical this is because you (or one of your users) are using an outdated version of safari that officially supports only SOME of the latest and greatest javascript improvements. Upgrading Safari may also take care of this issue, depending on when you read this.

Cheers

like image 142
Jacksonkr Avatar answered Sep 23 '22 01:09

Jacksonkr