No visible cause for "Unexpected token ILLEGAL"
Uncaught SyntaxError: Unexpected token ILLEGAL
This is my code:
var foo = 'bar';
It's super simple, as you can see. How could it be causing a syntax error?
The same error is raised if, for example, you try to run a js file with a rogue
@ character, a misplaced curly brace, bracket, "smart quotes", single quotes not enclosed properly (e.g.
this.run('dev1)) and so on.
A lot of different situations can cause this error. But if you don't have any obvious syntax error or illegal character, it may be caused by an invisible illegal character. That's what this answer is about.
But I can't see anything illegal!
There is an invisible character in the code, right after the semicolon. It's the Unicode
U+200B Zero-width space character (a.k.a.
ZWSP, HTML entity
​). That character is known to cause the
And where did it come from?
I can't tell for sure, but my bet is on jsfiddle. If you paste code from there, it's very likely to include one or more
U+200B characters. It seems the tool uses that character to control word-wrapping on long strings.
After the latest jsfiddle update, it's now showing the character as a red dot like codepen does. Apparently, it's also not inserting
U+200Bcharacters on its own anymore, so this problem should be less frequent from now on.
Vagrant appears to sometimes cause this issue as well, due to a bug in VirtualBox. The solution, as per this blog post is to set
sendfile off;in your nginx config, or
EnableSendfile Offif you use Apache.
It's also been reported that code pasted from the Chrome developer tools may include that character, but I was unable to reproduce that with the current version (22.0.1229.79 on OSX).
How can I spot it?
The character is invisible, do how do we know it's there? You can ask your editor to show invisible characters. Most text editors have this feature. Vim, for example, displays them by default, and the
ZWSP shows as
<u200b>. You can also debug it online: jsbin displays the character as a red dot on its code panes (but seems to remove it after saving and reloading the page). CodePen.io also displays it as a dot, and keeps it even after saving.
That character is not something bad, it can actually be quite useful. This example on Wikipedia demonstrates how it can be used to control where a long string should be wrapped to the next line. However, if you are unaware of the character's presence on your markup, it may become a problem. If you have it inside of a string (e.g., the
nodeValue of a DOM element that has no visible content), you might expect such string to be empty, when in fact it's not (even after applying
ZWSP can also cause extra whitespace to be displayed on an HTML page, for example when it's found between two
<div> elements (as seen on this question). This case is not even reproducible on jsfiddle, since the character is ignored there.
Another potential problem: if the web page's encoding is not recognized as UTF-8, the character may actually be displayed (as
â€‹ in latin1, for example).
ZWSP is present on CSS code (inline code, or an external stylesheet), styles can also not be parsed properly, so some styles don't get applied (as seen on this question).
The ECMAScript Specification
I couldn't find any mention to that specific character on the ECMAScript Specification (versions 3 and 5.1). The current version mentions similar characters (
U+200D) on Section 7.1, which says they should be treated as
IdentifierParts when "outside of comments, string literals, and regular expression literals". Those characters may, for example, be part of a variable name (and
var x\u200c; indeed works).
Section 7.2 lists the valid White space characters (such as tab, space, no-break space, etc.), and vaguely mentions that any other Unicode “space separator” (category “Zs”) should be treated as white space. I'm probably not the best person to discuss the specs in this regard, but it seems to me that
U+200B should be considered white space according to that, when in fact the implementations (at least Chrome and Firefox) appear to treat them as an unexpected token (or part of one), causing the syntax error.
Read more... Read less...
why you looking for this problem into your code? Even, if it's copypasted.
If you can see, what exactly happening after save file in synced folder - you will see something like
***** at the end of file. It's not related to your code at all.
If you are using
nginx in vagrant box - add to server config:
If you are using
apache in vagrant box - add to server config:
Source of problem: VirtualBox Bug
This also could be happening if you're copying code from another document (like a PDF) into your console and trying to run it.
Apparently, copying from the PDF introduces some unexpected, illegal, and invisible characters into the code.
I got this error in chrome when I had an unterminated string after the line that the error pointed to. After closing the string the error went away.
Example with error:
Example without error:
To fix this I had to change the settings on my mac System Preferences=>Keyboard=>Text(tab) uncheck use smart quotes and dashes (default was checked).
If you are running a nginx + uwsgi setup vagrant then the main problem is the Virtual box bug with send file as mentioned in some of the answers. However to resolve it you have to disable sendfile in both nginx and uwsgi.
In nginx.conf sendfile off
uwsgi application / config --disable-sendfile