require()从JavaScript文件或REPL获取CoffeeScript文件 [英] require()'ing a CoffeeScript file from a JavaScript file or REPL

查看:90
本文介绍了require()从JavaScript文件或REPL获取CoffeeScript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Node.js并且想要将CoffeeScript加入我的工作流程中。我有两个用例:

I'm using Node.js and wanting to incorporate CoffeeScript into my workflow. I have two use-cases:


  1. 我想要能够编写 require code> CoffeeScript modules

  2. 我想要从节点REPL中加载CoffeeScript模块

对于case#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()

有更好,更统一的方法吗?

Is there a better, more unified way to do this?

推荐答案

咖啡脚本模块需要注册其扩展。

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');

这篇关于require()从JavaScript文件或REPL获取CoffeeScript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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