我得到这个JavaScript错误在我的控制台:

未捕获SyntaxError:意外令牌非法

这是我的代码:

Var foo = 'bar';

正如你所看到的,这非常简单。它怎么会导致语法错误呢?


当前回答

当运行OS X时,文件系统会为基本上所有文件创建隐藏的分叉,如果它们位于不支持HFS+的硬盘驱动器上。这有时会导致你的JavaScript引擎试图运行data-fork,而不是你想让它运行的代码。当这发生时,你也会收到

SyntaxError: Unexpected token ILLEGAL

因为你的文件的数据分支将包含Unicode U+200B字符。删除数据分叉文件将使脚本运行实际的、预期的代码,而不是代码的二进制数据分叉。

.whatever : These files are created on volumes that don't natively support full HFS file characteristics (e.g. ufs volumes, Windows fileshares, etc). When a Mac file is copied to such a volume, its data fork is stored under the file's regular name, and the additional HFS information (resource fork, type & creator codes, etc) is stored in a second file (in AppleDouble format), with a name that starts with ".". (These files are, of course, invisible as far as OS-X is concerned, but not to other OS's; this can sometimes be annoying...)

戈登·戴维森@ http://www.westwind.com/reference/OS-X/invisibles.html

其他回答

如果你正在运行一个nginx + uwsgi安装流浪者,那么主要的问题是在一些答案中提到的发送文件的虚拟盒错误。然而,要解决这个问题,你必须在nginx和uwsgi中禁用sendfile。

在nginx.conf sendfile掉 Uwsgi应用程序/配置 ——disable-sendfile

我在我的mac上也有同样的问题,发现这是因为mac正在用不合法的javascript字符替换标准引号。

为了解决这个问题,我不得不改变我的mac上的设置系统首选项=>键盘=>文本(选项卡)取消勾选使用智能引号和破折号(默认勾选)。

为什么要在代码中寻找这个问题?甚至,如果它是复制粘贴。

如果你能看到,在同步文件夹保存文件后到底发生了什么-你会在文件末尾看到类似*****的东西。这和你的代码一点关系都没有。

解决方案。

如果你在vagrant box中使用nginx -添加到服务器配置:

sendfile off;

如果你在vagrant box中使用apache -添加到服务器配置:

EnableSendfile Off;

问题来源:VirtualBox Bug

我也遇到了同样的问题,因为我在文本字符串中添加代码时按了enter键。

因为它是一个很长的文本字符串,我想看到它,而不必在我的文本编辑器中滚动,然而,按enter添加了一个不可见的字符字符串,这是非法的。我使用Sublime Text作为编辑器。

当运行OS X时,文件系统会为基本上所有文件创建隐藏的分叉,如果它们位于不支持HFS+的硬盘驱动器上。这有时会导致你的JavaScript引擎试图运行data-fork,而不是你想让它运行的代码。当这发生时,你也会收到

SyntaxError: Unexpected token ILLEGAL

因为你的文件的数据分支将包含Unicode U+200B字符。删除数据分叉文件将使脚本运行实际的、预期的代码,而不是代码的二进制数据分叉。

.whatever : These files are created on volumes that don't natively support full HFS file characteristics (e.g. ufs volumes, Windows fileshares, etc). When a Mac file is copied to such a volume, its data fork is stored under the file's regular name, and the additional HFS information (resource fork, type & creator codes, etc) is stored in a second file (in AppleDouble format), with a name that starts with ".". (These files are, of course, invisible as far as OS-X is concerned, but not to other OS's; this can sometimes be annoying...)

戈登·戴维森@ http://www.westwind.com/reference/OS-X/invisibles.html