URIError
JavaScript Programming Language
Severity: MinorWhat Does This Error Mean?
A URIError means you passed an invalid web address (URI) to one of JavaScript's built-in URI functions. Functions like decodeURI() and decodeURIComponent() expect properly formatted addresses. If the text you pass contains characters that are not valid in a URI, JavaScript throws this error.
Affected Models
- All browsers
- Node.js
- Deno
- Any JavaScript runtime
Common Causes
- Passing a malformed or partially encoded string to decodeURI() or decodeURIComponent()
- Trying to decode a string that was not properly encoded in the first place
- A URL contains a percent sign (%) not followed by two valid hex digits
- Manually building a URL string with special characters that need to be encoded first
- Copying a URL from an application that uses a different encoding format
How to Fix It
-
Check what string you are passing to decodeURI() or decodeURIComponent(). Log it with console.log() to see its exact contents.
Look for a lone percent sign (%) not followed by two letters or numbers — that is an invalid escape sequence.
-
Make sure you are using the right function. Use encodeURIComponent() to encode individual parts of a URL (like a parameter value), then decodeURIComponent() to decode them.
Do not use encodeURI() on individual parameters — it will not encode special characters like & and = which are needed for URL structure.
-
Wrap the decode call in a try/catch block so a bad URL does not crash your whole application.
Example: try { let decoded = decodeURIComponent(input); } catch (e) { console.error('Bad URL:', e); }
-
If you are building a URL with user input, always encode it first with encodeURIComponent() before putting it in the URL.
User input can contain spaces, special characters, or international letters that are not valid in a raw URL.
-
If you received the URL from an external source (API, database, user input), validate it before passing it to decode functions.
A simple check: if the string contains '%' followed by something that is not two hex characters, it is malformed.
When to Call a Professional
URIErrors are minor and fixable without professional help. They are almost always caused by an incorrectly formatted string being passed to a URI function. Check the string value and encoding, and the error will resolve.
Frequently Asked Questions
What is the difference between encodeURI and encodeURIComponent?
encodeURI() is for encoding a full URL. It leaves characters like /, ?, &, and = alone because they are part of URL structure. encodeURIComponent() is for encoding a single piece of data that goes inside a URL, like a search term or a parameter value. It encodes those special characters too, which is what you want when the data itself might contain them.
How do I safely decode a URL that might be invalid?
Always wrap decodeURIComponent() in a try/catch block. This way, if the URL is malformed, your code catches the URIError instead of crashing. You can then show an error message or use a fallback value instead of letting the page break.
Is URIError common?
It is less common than TypeError or ReferenceError. It typically appears when working with URL parameters, query strings, or data fetched from an API. If you always encode data before putting it in URLs and always use try/catch when decoding, you will rarely encounter this error.