咖啡脚本意外令牌非法,但不应该有任何非法

Coffeescript unexpected token ILLEGAL, but there shouldn't be anything illegal

本文关键字:非法 不应该 任何 意外 令牌 咖啡 脚本      更新时间:2023-09-26

这真的很令人生气。我在我的代码中找不到我正在做任何非法事情的地方,但由于某种原因,调用fork会炸毁我的程序。这是代码。相关部分在 svgToPNG 中,我称之为fork

{fork} = require 'child_process'
{Coral} = require 'coral'
svgToPNG = (svg, reply, log) ->
  log "converting SVG to a PNG"
  # set up a child process to call convert svg: png:-
  convert = fork '/usr/bin/env', ['convert', 'svg:', 'png:-']
  log "Spawned child process running convert, pid " + convert.pid
  # set up behavior when an error occurs
  convert.stderr.on "data", ->
    log "Error occurred while executing convert"
    reply "error"
  # set up behavior for when we successfully convert
  convert.stdout.on "data", ->
    log "Successful conversion! :)"
    log "here's the data: " + data
    reply data
  # pipe the SVG into the stdin of the process (starting it)
  convert.stdin.end svg

如果我把fork线拿出来,用别的东西代替它,一切都是笨拙的dory,但如果我把它留在里面,我会得到:

> coffee src/coral_client.coffee
finished doing conversion to svg!
converting SVG to a PNG
Spawned child process running convert, pid 60823
/usr/bin/grep:1
(function (exports, require, module, __filename, __dirname) { ����
                                                              ^
SyntaxError: Unexpected token ILLEGAL
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3

这毫无意义。我没有任何像这个问题那样奇怪的非法 unicode 字符,我不相信我有任何像这个问题这样的解析错误......我真的不知道发生了什么。

可能是CoffeeScript以某种方式破坏了代码吗?这似乎不太可能,但我不知道。

错误在于您使用forkfork用于生成节点进程,即 foo.js文件。 请改用spawn

我通过运行代码的精简版本,读取图像文件然后将其传递给您的svgToPNG来解决这个问题。错误消息开始:

/usr/bin/env:1
(function (exports, require, module, __filename, __dirname) { ELF

在此复制/粘贴中呈现为ELF字符是我的二进制/usr/bin/env文件的头字符。 所以node.js fork正在尝试编译/usr/bin/env文件。 查看child_process文档证实了这一点。 运行 lsgrep 等内容的示例使用 spawn