【问题标题】:flatironjs and Cloud9; window is undefined error?flatironjs 和 Cloud9;窗口未定义错误?
【发布时间】:2012-03-16 23:07:28
【问题描述】:

还有其他人在 Cloud9 ide 中使用 flatironjs 吗?

在我的 server.js 文件中,我有:

require("coffee-script"); 
var app = require("./app");

app.listen(process.env.PORT);

然后在我的 app.coffee 文件中:

flatiron = require "flatiron"
director = require "director"

app = flatiron.app
app.use flatiron.plugins.http

module.exports = app.router.get "/", ->
res.writeHead 200, { "Content-Type": "text/plain" }
res.end "Hello world!\n"

当我尝试在 Cloud9 IDE 中运行它时,我得到以下信息:

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        **^ ReferenceError: window is not defined**
    at Object. (/node_modules/flatiron/node_modules/broadway/node_modules/eventemitter2/lib/eventemitter2.js:547:63)
    at Module._compile (module.js:411:26)
    at Object..js (module.js:417:10)
    at Module.load (module.js:343:31)
    at Function._load (module.js:302:12)
    at require (module.js:355:19)
    at Object. (/node_modules/flatiron/node_modules/broadway/lib/broadway/app.js:11:14)
    at Module._compile (module.js:411:26)
    at Object..js (module.js:417:10)
    at Module.load (module.js:343:31)

如果我在不使用 flatiron 的情况下创建标准 http 服务器,一切都会运行良好:

http = require "http"
module.exports = http.createServer (req, res) ->
res.writeHead 200, {'Content-Type': 'text/plain'}
res.end "Hello World\n"

想法?

【问题讨论】:

    标签: node.js cloud9-ide flatiron.js


    【解决方案1】:

    这是在 Solaris 上运行的 EventEmitter 中的一个错误。如果您只是在最新版本的 Solaris 上运行应用程序,您也可以看到它,将崩溃并显示相同的错误消息。您可以使用删除浏览器检查的patched EventEmitter2

    我为你创建了an issue

    【讨论】:

    • 所以,得到了修补的 EventEmitter2,它让我克服了原始窗口未定义的问题。但是,现在我收到“无法调用未定义的方法'listen'”错误。
    【解决方案2】:

    所以bottom of that eventemitter2.js file 上有一些代码基本上试图“同构”并在 node.js 和浏览器中工作。它试图通过测试以下定义的全局变量来猜测哪个环境:

    • 进程
    • process.title
    • 出口

    如果所有这些都已定义,eventemitter2 会将其导出的属性附加到 exports 对象以在 node.js 中使用。否则,它会将它们附加到 window 对象以在浏览器中使用。

    由于 cloud9 内部的某种原因,这 3 个全局变量中的 1 个或多个未定义,并且假设 window 存在并且失败,它会分支到“浏览器”模式。我对 cloud9 ide 托管环境了解得不够多,无法准确了解它是哪一个(或 2 个或 3 个)以及它为什么丢失。

    您的原版http 代码可以正常工作,因为它不加载 eventemitter2,它是在您使用 flatiron 时加载的,这取决于百老汇,这取决于 eventemitter2。

    【讨论】:

    • 对,我遵循。我更好奇使用 Cloud9 的人是否能够启动并运行熨斗。我已经确认进程对象可用。但是,由于错误状态,窗口对象未定义...
    • process.title 和exports 怎么样?
    • process.title 似乎为空白。当我尝试 console.log 时,它什么也没有出现,但它不是未定义的。出口不是未定义的。
    • 嗯。诡异的。我猜想通过电子邮件或 IRC 尝试 cloud9 的家伙。
    • 这里是 Cloud9 的家伙。我们也发现了这一点,因为我们永远使用某些服务,而且这些服务也不会再运行了。我们已经发布了 EventEmitter2 的补丁版本,并报告了该项目的一个错误。
    猜你喜欢
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    • 2021-12-04
    • 2021-08-23
    • 1970-01-01
    • 2019-11-07
    相关资源
    最近更新 更多