Typeerror: Cannot Assign to Read Only Property 'exports' of Object '#<object>'
JavaScript Errors and How to Prepare Them
JavaScript can be a nightmare to debug: Some errors it gives can exist very difficult to empathise at get-go, and the line numbers given aren't always helpful either. Wouldn't it be useful to take a list where you could look to find out what they mean and how to fix them? Here you lot go!
Beneath is a list of the strange errors in JavaScript. Unlike browsers can requite you dissimilar letters for the same mistake, so there are several unlike examples where applicable.
How to read errors?
Before the list, let's chop-chop look at the construction of an error message. Understanding the structure helps understand the errors, and you'll have less trouble if you run into any errors non listed here.
A typical error from Chrome looks like this:
Uncaught TypeError: undefined is not a function
The construction of the mistake is as follows:
- Uncaught TypeError: This part of the message is usually not very useful. Uncaught means the error was not caught in a
take hold of
argument, andTypeError
is the error's proper noun. - undefined is not a function: This is the message part. With error messages, you lot accept to read them very literally. For example in this case it literally means that the lawmaking attempted to use
undefined
like it was a role.
Other webkit-based browsers, similar Safari, give errors in a similar format to Chrome. Errors from Firefox are similar, but do not always include the first function, and recent versions of Cyberspace Explorer also give simpler errors than Chrome – but in this instance, simpler does non ever hateful meliorate.
Now onto the actual errors.
Uncaught TypeError: undefined is not a office
Related errors: number is not a function, object is not a function, string is not a office, Unhandled Error: 'foo' is not a function, Function Expected
Occurs when attempting to call a value like a role, where the value is non a function. For example:
var foo = undefined; foo();
This error typically occurs if you lot are trying to call a function in an object, but you typed the name wrong.
var x = certificate.getElementByID('foo');
Since object backdrop that don't exist are undefined
by default, the above would result in this error.
The other variations such as "number is non a role" occur when attempting to call a number like it was a function.
How to fix this error: Ensure the function proper noun is correct. With this error, the line number will ordinarily betoken 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'
Acquired by attempting to assign a value to something that cannot be assigned to.
The most mutual example of this error is with if-clauses:
if(doSomething() = 'somevalue')
In this example, the programmer accidentally used a single equals instead of two. The message "left-hand side in assignment" is referring to the part on the left side of the equals sign, then like you can see in the above instance, the left-paw side contains something yous tin can't assign to, leading to the error.
How to fix this error: Make certain yous're not attempting to assign values to function results or to the this
keyword.
Uncaught TypeError: Converting round structure to JSON
Related errors: Uncaught exception: TypeError: JSON.stringify: Non an acyclic Object, TypeError: cyclic object value, Circular reference in value statement not supported
Always acquired by a circular reference in an object, which is and 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 instance have a reference to each other, the resulting object cannot exist converted into JSON.
How to prepare this error: Remove circular references like in the example from any objects you lot want to convert into JSON.
Unexpected token ;
Related errors: Expected ), missing ) after argument listing
The JavaScript interpreter expected something, but it wasn't at that place. Typically caused past mismatched parentheses or brackets.
The token in this error tin vary – it might say "Unexpected token ]" or "Expected {" etc.
How to fix this error: Sometimes the line number with this fault doesn't point to the right identify, making it difficult to fix.
- An error with [ ] { } ( ) is normally caused past a mismatching pair. Cheque that all your parentheses and brackets have a matching pair. In this example, line number will ofttimes signal to something else than the trouble character
- Unexpected / is related to regular expressions. The line number for this will usually be correct.
- Unexpected ; is unremarkably caused by having a ; inside an object or array literal, or within the argument list of a function telephone call. The line number will usually be right for this case too
Uncaught SyntaxError: Unexpected token ILLEGAL
Related errors: Unterminated String Literal, Invalid Line Terminator
A cord literal is missing the closing quote.
How to fix this error: Ensure all strings have the correct closing quote.
Uncaught TypeError: Cannot read property 'foo' of nil, Uncaught TypeError: Cannot read property 'foo' of undefined
Related errors: TypeError: someVal is null, Unable to get holding 'foo' of undefined or cypher reference
Attempting to read zippo
or undefined
as if it was an object. For example:
var someVal = null; panel.log(someVal.foo);
How to set up this error: Ordinarily caused by typos. Check that the variables used well-nigh the line number pointed by the error are correctly named.
Uncaught TypeError: Cannot set property 'foo' of null, Uncaught TypeError: Cannot set property 'foo' of undefined
Related errors: TypeError: someVal is undefined, Unable to set property 'foo' of undefined or null reference
Attempting to write null
or undefined
as if it was an object. For example:
var someVal = aught; someVal.foo = one;
How to prepare this mistake: This too is commonly 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, likewise much recursion, Stack overflow
Usually caused by a bug in program logic, causing infinite recursive function calls.
How to set up this error: Check recursive functions for bugs that could cause them to keep recursing forever.
Uncaught URIError: URI malformed
Related errors: URIError: malformed URI sequence
Caused by an invalid decodeURIComponent telephone call.
How to gear up this error: Check that the decodeURIComponent
call at the error'due south line number gets correct input.
XMLHttpRequest cannot load http://some/url/. No 'Admission-Control-Allow-Origin' header is nowadays on the requested resource
Related errors: Cantankerous-Origin Request Blocked: The Same Origin Policy disallows reading the remote resources at http://some/url/
This error is always caused by the usage of XMLHttpRequest.
How to ready this error: Ensure the request URL is correct and information technology respects the same-origin policy. A good manner to find the offending code is to look at the URL in the mistake bulletin and find it from your lawmaking.
InvalidStateError: An attempt was made to use an object that is non, or is no longer, usable
Related errors: InvalidStateError, DOMException code 11
Means the code called a function that yous should not telephone call at the current state. Occurs unremarkably with XMLHttpRequest
, when attempting to phone call functions on information technology before it'southward ready.
var xhr = new XMLHttpRequest(); xhr.setRequestHeader('Some-Header', 'val');
In this case, you would become the mistake because the setRequestHeader
function can simply be called after calling xhr.open up
.
How to set up this fault: Await at the code on the line pointed by the fault and brand certain information technology runs at the right time, or add any necessary calls earlier information technology (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 first to make more sense. Modern browsers likewise assist, every bit they no longer give the completely useless errors they used to.
What's the most confusing fault you've seen? Share the frustration in the comments!
Desire to learn more virtually these errors and how to forestall them? Find Issues in JavaScript Automatically with ESLint.
About Jani Hartikainen
Jani Hartikainen has spent over 10 years building web applications. His clients include companies like Nokia and hot super surreptitious startups. When non programming or playing games, Jani writes most JavaScript and loftier quality lawmaking on his site.
codeutopia.netjhartikainenPosts
Source: https://davidwalsh.name/fix-javascript-errors
0 Response to "Typeerror: Cannot Assign to Read Only Property 'exports' of Object '#<object>'"
Post a Comment