require()#39;ing a CoffeeScript file from a JavaScript file or REPL(从 JavaScript 文件或 REPL 中 require()ing CoffeeScript 文件)
问题描述
我正在使用 Node.js 并希望将 CoffeeScript 合并到我的工作流程中.我有两个用例:
I'm using Node.js and wanting to incorporate CoffeeScript into my workflow. I have two use-cases:
- 我希望能够编写
require()
CoffeeScript 模块的 JavaScript 文件 - 我希望能够从节点 REPL 中加载 CoffeeScript 模块
- I want to be able to write JavaScript files which
require()
CoffeeScript modules - I want to be able to load CoffeeScript modules from within the node REPL
对于案例 #1: 我可以从 .coffee
编译为 .js
和 require()
.js
模块,作为一种解决方法.
For case #1: I can just compile from .coffee
to .js
and require()
the .js
module, as a workaround.
对于案例 #2: 现在我正在 eval()
处理 coffee-script.compile()
的输出.
For case #2: Right now I'm eval()
ing the output of coffee-script.compile()
.
有没有更好、更统一的方法来做到这一点?
Is there a better, more unified way to do this?
推荐答案
coffee-script 模块在需要时注册其扩展.
The coffee-script module registers its extension once required.
$ echo 'console.log "works"' > module.coffee
$ echo '
> require("coffee-script")
> require("./module")
> ' > test.js
$ node test.js
works
$ node
> require('coffee-script'); require('./module')
works
{}
编辑:这种行为随着 CoffeeScript 1.7.0 的发布而改变.现在你需要做:
Edit: This behaviour has changed with the relase of CoffeeScript 1.7.0. Now you need to do:
require('coffee-script/register');
这篇关于从 JavaScript 文件或 REPL 中 require()'ing CoffeeScript 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 JavaScript 文件或 REPL 中 require()'ing CoffeeScript 文件


- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01