如何让 TextMate 看到“需要”编译时 CoffeeScript 中的表达式?
当在 TextMate 中编写 CoffeeScript 并想要使用第三方 Javascript 库(如 jQuery 或 Raphael)时,需要添加一个“require”,如下所示:
$ = require 'jquery'
当使用 coffee -c myfile.coffee
时,效果很好命令行,但在尝试编译时在 TextMate 包中失败,并出现以下错误:
Error: Cannot find module 'jquery'
at Function._resolveFilename (module.js:317:11)
at Function._load (module.js:262:25)
at require (module.js:346:19)
at Object. (.:3:7)
at Object. (.:4:4)
at Module._compile (module.js:402:26)
at Object.run (/usr/lib/node_modules/coffee-script/lib/coffee-script.js:62:19)
at /usr/lib/node_modules/coffee-script/lib/command.js:120:29
at Socket. (/usr/lib/node_modules/coffee-script/lib/command.js:154:14)
at Socket.emit (events.js:61:17)
在此示例中,代码全部位于项目根目录的同一目录中。
当指定以下内容时也会发生同样的情况:
$ = require 'jquery.js'
其他人如何使用 TextMate 包在 CoffeeScript 中进行编译?对于我来说,除了最琐碎的代码之外,这似乎都是一个阻碍。除了语法突出显示之外,这肯定是这个包中最重要的部分之一吗?
When writing CoffeeScript in TextMate and wanting to use a 3rd party Javascript library like jQuery or Raphael there is a need to add a "require" like so:
$ = require 'jquery'
This works fine when using coffee -c myfile.coffee
from the commandline, but fails in the TextMate bundle when trying to compile with this error:
Error: Cannot find module 'jquery'
at Function._resolveFilename (module.js:317:11)
at Function._load (module.js:262:25)
at require (module.js:346:19)
at Object. (.:3:7)
at Object. (.:4:4)
at Module._compile (module.js:402:26)
at Object.run (/usr/lib/node_modules/coffee-script/lib/coffee-script.js:62:19)
at /usr/lib/node_modules/coffee-script/lib/command.js:120:29
at Socket. (/usr/lib/node_modules/coffee-script/lib/command.js:154:14)
at Socket.emit (events.js:61:17)
In this example, the code is all in the same directory at the root of the project.
The same occurs when specifying:
$ = require 'jquery.js'
How are other people compiling in CoffeeScript with the TextMate bundle? This would seem to be a showstopper to me for anything but the most trivial code. Apart from syntax highlighting, surely this is one of the most important parts of this bundle?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已点击“运行”命令 (⌘R),相当于
coffee myfile.coffee
。您需要“编译并显示 JS”(⌘B),相当于
coffee -c --print --bare myfile.coffee
。这应该会打开一个带有编译输出的窗口至于像 jQuery 和 Raphael 这样的
require
库,我希望您理解require
只是一个用于加载模块的运行时函数;它没有编译时意义。您可能来自 C++ 或 Java 等带有“链接器”的语言。另一方面,在 CoffeeScript 中,代码一次被编译为 JavaScript 文件,并且 JavaScript 文件被单独加载到运行时环境(无论是浏览器还是 Node.js 等框架)。You've hit the "Run" command (⌘R), equivalent to
coffee myfile.coffee
.You want "Compile and Display JS" (⌘B), equivalent to
coffee -c --print --bare myfile.coffee
. This should open up a window with the compiled outputAs to
require
-ing libraries like jQuery and Raphael, I hope you understand thatrequire
is just a runtime function for loading modules; it has no compile-time meaning. You may be coming from a language like C++ or Java where there's a "linker." In CoffeeScript, on the other hand, code is compiled to JavaScript one file at a time, and JavaScript files are loaded individually into the runtime environment (be it a browser or a framework like Node.js).