Skip to content Skip to sidebar Skip to footer

Failed to Compile. Typeerror: Cannot Read Property 'getsourcefile' of Null

JavaScript Errors and How to Gear up Them

JavaScript tin can exist a nightmare to debug: Some errors it gives can exist very difficult to understand at get-go, and the line numbers given aren't ever helpful either. Wouldn't it be useful to have a listing where y'all could look to find out what they hateful and how to fix them? Hither you lot go!

Below is a list of the strange errors in JavaScript. Different browsers can give you different messages for the same error, so at that place are several dissimilar examples where applicable.

How to read errors?

Before the listing, let's quickly wait at the structure of an error bulletin. Agreement the construction helps empathize the errors, and you'll have less problem if you lot run into any errors not listed here.

A typical error from Chrome looks like this:

Uncaught TypeError: undefined is not a function

The structure of the error is as follows:

  1. Uncaught TypeError: This role of the message is usually non very useful. Uncaught ways the error was not caught in a take hold of statement, and TypeError is the error's name.
  2. undefined is not a function: This is the message part. With error letters, you lot have to read them very literally. For instance in this case it literally ways that the lawmaking attempted to use undefined like information technology was a function.

Other webkit-based browsers, like Safari, give errors in a similar format to Chrome. Errors from Firefox are like, simply exercise non ever include the first office, and recent versions of Internet Explorer besides give simpler errors than Chrome – just in this instance, simpler does non always mean improve.

Now onto the actual errors.

Uncaught TypeError: undefined is not a role

Related errors: number is not a role, object is non a function, string is non a part, Unhandled Error: 'foo' is not a function, Part Expected

Occurs when attempting to call a value like a part, where the value is not a function. For case:

var foo = undefined; foo();

This error typically occurs if yous are trying to phone call a function in an object, merely you lot typed the name incorrect.

var x = document.getElementByID('foo');

Since object properties that don't exist are undefined by default, the above would outcome in this mistake.

The other variations such every bit "number is not a part" occur when attempting to telephone call a number like it was a part.

How to fix this error: Ensure the function name is correct. With this mistake, the line number volition unremarkably point at the right location.

Uncaught ReferenceError: Invalid left-hand side in assignment

Related errors: Uncaught exception: ReferenceError: Cannot assign to 'functionCall()', Uncaught exception: ReferenceError: Cannot assign to 'this'

Caused by attempting to assign a value to something that cannot be assigned to.

The nigh mutual example of this error is with if-clauses:

if(doSomething() = 'somevalue')

In this example, the programmer accidentally used a unmarried equals instead of 2. The message "left-manus side in consignment" is referring to the part on the left side of the equals sign, and so like y'all tin can see in the above example, the left-hand side contains something you can't assign to, leading to the error.

How to set up this fault: Make sure y'all're non attempting to assign values to role results or to the this keyword.

Uncaught TypeError: Converting circular structure to JSON

Related errors: Uncaught exception: TypeError: JSON.stringify: Not an acyclic Object, TypeError: cyclic object value, Circular reference in value argument not supported

Ever acquired by a round reference in an object, which is then passed into JSON.stringify.

var a = { }; var b = { a: a }; a.b = b; JSON.stringify(a);

Because both a and b in the above example take a reference to each other, the resulting object cannot exist converted into JSON.

How to set this error: Remove circular references similar in the example from whatsoever objects you want to convert into JSON.

Unexpected token ;

Related errors: Expected ), missing ) after statement listing

The JavaScript interpreter expected something, but it wasn't there. Typically caused by mismatched parentheses or brackets.

The token in this fault can vary – information technology might say "Unexpected token ]" or "Expected {" etc.

How to gear up this error: Sometimes the line number with this mistake doesn't point to the correct identify, making it hard to set up.

  • An error with [ ] { } ( ) is unremarkably caused by a mismatching pair. Cheque that all your parentheses and brackets have a matching pair. In this case, line number will oft betoken to something else than the trouble character
  • Unexpected / is related to regular expressions. The line number for this will commonly be correct.
  • Unexpected ; is usually acquired by having a ; within an object or array literal, or within the statement list of a function phone call. The line number will usually be correct for this case every bit well

Uncaught SyntaxError: Unexpected token ILLEGAL

Related errors: Unterminated String Literal, Invalid Line Terminator

A string literal is missing the closing quote.

How to fix this error: Ensure all strings accept the correct endmost quote.

Uncaught TypeError: Cannot read property 'foo' of zero, Uncaught TypeError: Cannot read property 'foo' of undefined

Related errors: TypeError: someVal is null, Unable to go holding 'foo' of undefined or null reference

Attempting to read zero or undefined equally if it was an object. For example:

var someVal = null; console.log(someVal.foo);

How to fix this error: Normally caused by typos. Check that the variables used near the line number pointed past the error are correctly named.

Uncaught TypeError: Cannot set belongings 'foo' of nil, Uncaught TypeError: Cannot set up property 'foo' of undefined

Related errors: TypeError: someVal is undefined, Unable to gear up property 'foo' of undefined or nothing reference

Attempting to write null or undefined as if information technology was an object. For example:

var someVal = nothing; someVal.foo = one;

How to fix this mistake: This too is usually caused by typos. Check the variable names near the line the error points to.

Uncaught RangeError: Maximum call stack size exceeded

Related errors: Uncaught exception: RangeError: Maximum recursion depth exceeded, too much recursion, Stack overflow

Commonly caused by a bug in program logic, causing space recursive function calls.

How to gear up this error: Check recursive functions for bugs that could crusade them to keep recursing forever.

Uncaught URIError: URI malformed

Related errors: URIError: malformed URI sequence

Acquired by an invalid decodeURIComponent call.

How to fix this error: Check that the decodeURIComponent telephone call at the error's line number gets correct input.

XMLHttpRequest cannot load http://some/url/. No 'Access-Command-Permit-Origin' header is present on the requested resource

Related errors: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://some/url/

This error is always acquired by the usage of XMLHttpRequest.

How to fix this fault: Ensure the request URL is right and it respects the same-origin policy. A skillful fashion to find the offending lawmaking is to look at the URL in the mistake message and find it from your code.

InvalidStateError: An try was fabricated to employ an object that is not, or is no longer, usable

Related errors: InvalidStateError, DOMException code 11

Means the lawmaking chosen a function that you should not telephone call at the current state. Occurs normally with XMLHttpRequest, when attempting to telephone call functions on it before information technology'southward gear up.

var xhr = new XMLHttpRequest(); xhr.setRequestHeader('Some-Header', 'val');

In this example, you would get the error because the setRequestHeader part can only be called later calling xhr.open.

How to set up this error: Look at the code on the line pointed past the error and make sure it runs at the correct time, or add together any necessary calls before it (such every bit xhr.open)

Conclusion

JavaScript has some of the nigh unhelpful errors I've seen, with the exception of the notorious Expected T_PAAMAYIM_NEKUDOTAYIM in PHP. With more familiarity the errors commencement to make more sense. Modern browsers besides aid, equally they no longer requite the completely useless errors they used to.

What'southward the well-nigh disruptive error you've seen? Share the frustration in the comments!

Want to larn more than nigh these errors and how to forestall them? Detect Issues in JavaScript Automatically with ESLint.

Website performance monitoring

Website performance monitoring

Jani Hartikainen

Virtually Jani Hartikainen

Jani Hartikainen has spent over 10 years edifice spider web applications. His clients include companies similar Nokia and hot super surreptitious startups. When not programming or playing games, Jani writes about JavaScript and high quality code on his site.

macdonaldhougmenseed.blogspot.com

Source: https://davidwalsh.name/fix-javascript-errors

Post a Comment for "Failed to Compile. Typeerror: Cannot Read Property 'getsourcefile' of Null"