coffeescript中的NodeJS'Require'加载库,Javascript中的空对象 [英] NodeJS 'Require' loading library in coffeescript, empty object in Javascript

查看:79
本文介绍了coffeescript中的NodeJS'Require'加载库,Javascript中的空对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了NodeJS中一个有趣的问题,该问题不时弹出,然后在工作中,但我一直无法弄清楚.

I've come across an interesting problem in NodeJS that pops up now and then at work, and I haven't been able to figure it out.

我用CoffeeScript编写了一个后端,然后将它与grunt-contrib-coffee一起编译为Java脚本,该脚本位于〜/bin目录中.我还提供了一个库,该库带有相应的私钥在Bitbucket上私下托管,并通过npm安装.该库也是用coffeescript编写的.

I've written a back-end in CoffeeScript, which I then compile with grunt-contrib-coffee into Javascript in a ~/bin directory. I also include a library that I privately host on Bitbucket with the appropriate private keys, and install through npm. This library too is written in coffeescript.

通常,我可以像使用任何其他库一样使用一个简单的要求,将其包含在Javascript中,而不会遇到任何麻烦.但是,偶尔使用后端的一台服务器获取更新,并且停止工作.当我检查错误时,它始终是相同的-传递了'require',但不是在JavaScript中加载实际的库,而是返回一个空对象({}).在coffeescript中运行代码仍然可行,但是无论我做什么-重新编译,重新安装所有依赖项,删除并克隆存储库,问题仍然存在.

Usually I'm able to include this library in Javascript without any headaches, using a simple require just like I would for any other library. However, occasionally one of the servers that's using the back-end gets updates, and it stops working. When I go check the error, it's always the same - 'require' passes, but instead of loading the actual library in JavaScript, it returns an empty object ({}). Running the code in coffeescript still works, but regardless of what I do - recompile, reinstall all dependencies, remove and clone the repository, the problem persists.

我对自己可能是什么的想法已经用完了,所以我希望这里的人以前遇到过这个问题,也许可以为我指明正确的方向.

I've run out of ideas of what it might be myself, so I'm hoping that someone here has come across the problem before and might be able to point me in the right direction.

在库package.json中:

In the library package.json:

{ 
    "name": "graph-engine",
    "main": "./lib/graph"
}

在图书馆的graph.coffee中

In the library's graph.coffee

class Graph
    constructor: () ->
      # Perform init

module.exports = Graph

然后在应用程序的package.json中:

Then in the app's package.json:

{
    "graph-engine": "git+ssh://git@bitbucket.org:<team>/graph-engine.git"
}

最后,在应用程序本身中:

Finally, in the app itself:

GraphEngine = require "graph-engine"
engineInstance = new GraphEngine()

这在coffeescript中可以正常工作,但是在使用grunt-contrib-coffee的以下设置使用grunt编译应用程序时:

This works fine in coffeescript, but when compiling the app using grunt with the following setup for grunt-contrib-coffee:

coffee:
  glob_to_multiple: 
    expand: true
    flatten: false
    cwd: 'src'
    src: ['**/*.coffee']
    dest: 'bin'
    ext: '.js'

在运行已编译的应用程序时,它无法正确加载库,而是返回一个空对象.再次强调一下,这种情况并非总是会发生,因此我没有包含任何代码或json文件,因为我认为它们是无关的.

It fails to load the library correctly when running the compiled application, instead returning an empty object. Again, I'd like to emphasise that this does not always happen, and as such I didn't include any code or json files as I believed that it was unrelated.

推荐答案

尽管到目前为止,行为的随机性的确切原因仍在我的脑海中,但我发现它与这些用CoffeeScript编写的库有关.

Although the exact reason for the randomness of the behaviour eludes me to this day, I have found that it is related to these libraries being written in CoffeeScript.

似乎,根据加载顺序,第三方库有时会注册coffee脚本,而我自己的库会正确加载,而其他时候,这些库将在我自己的库加载后加载,导致无法加载coffeescript.因此,该解决方案非常简单-注册coffeescript.可以通过在node.js应用程序的开始处放置以下内容来完成此操作:

It seems that depending on the load order, occasionally third party libraries would register coffee script and my own libraries would load correctly, whereas other times these libraries would load after my own libraries had loaded, resulting in an inability to load coffeescript. Therefore, the solution turned out to be fairly straightforward - register coffeescript. This can be done by putting the following at the start of the node.js app:

require('coffee-script/register')

有关更多信息,请参见文档.

Refer to the documentation for further information.

这篇关于coffeescript中的NodeJS'Require'加载库,Javascript中的空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆