这真是令人气愤。我在我的代码中找不到任何我在做非法事情的地方,但由于某种原因,调用 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 行并用其他东西替换它,一切都很好,但如果我把它留在里面,我会得到:
> 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
没有意义。我没有像 this question 中那样的任何奇怪的非法 unicode 字符,我不相信我有任何类型的解析错误,如 this one ...我真的不知道发生了什么。
会不会是 CoffeeScript 以某种方式破坏了代码?这似乎不太可能,但我不知道。
最佳答案
错误在于您使用fork。 fork 用于生成 Node 进程,即 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 文档可以确认这一点。运行 ls 和 grep 之类的示例使用 spawn。
关于javascript - Coffeescript unexpected token ILLEGAL,但不应该有任何非法内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19170607/