gpt4 book ai didi

javascript - 如何让 TextMate 在编译时看到 CoffeeScript 中的 "require"表达式?

转载 作者:行者123 更新时间:2023-11-28 21:19:36 27 4
gpt4 key购买 nike

当在 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 中进行编译?对于我来说,除了最琐碎的代码之外,这似乎都是一个阻碍。除了语法突出显示之外,这肯定是这个包中最重要的部分之一吗?

最佳答案

您已点击“运行”命令 (⌘R),相当于 coffee myfile.coffee

您需要“编译并显示 JS”(⌘B),相当于 coffee -c --print --bare myfile.coffee。这应该会打开一个包含编译输出的窗口

var $;
$ = require('jquery');

对于像 jQuery 和 Raphael 这样的 require 库,我希望你明白 require 只是一个加载模块的运行时函数;它没有编译时意义。您可能来自 C++ 或 Java 等带有“链接器”的语言。另一方面,在 CoffeeScript 中,代码一次被编译为 JavaScript 个文件,并且 JavaScript 文件被单独加载到运行时环境(无论是浏览器还是 Node.js 等框架)。

关于javascript - 如何让 TextMate 在编译时看到 CoffeeScript 中的 "require"表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6727886/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com