如何在coffeescript文件中使用node.js模块的“nodeunit”

我试图让一个coffeescript项目中的nodeunit模块工作,但似乎无法得到一个基本的testing运行。 这里是我的例子Coffeescript需要'nodeunit'

test = true test2 = false exports.testSomething = (test) -> test.expect(1) test.ok(true, "this should pass") test.done() exports.testSomethingElse = (test2) -> test2.expect(1) test2.ok(false, "this should fail") test2.done() 

不幸的是,当我运行'$ nodeunit example.coffee'时,我得到错误输出:

example.coffee:4 exports.testSomething =(test) – > ^

module.js:296 throw err; 在Module.load(module.js:334:31)上的Object.js(module.js:408:10)上​​的Module._compile(module.js:397:25)处的SyntaxError:Unexpected token> (module.js:293:12)在/ usr / local / lib / node /usr/local/lib/node/nodeunit/lib/nodeunit.js:75:37的require(module.js:346:19) /usr/local/lib/node/nodeunit/deps/async.js:118:13位于/usr/local/lib/node/nodeunit/deps/async.js处的/nodeunit/deps/async.js:508:13 :/usr/local/lib/node/nodeunit/deps/async.js:507:9:134:9

任何人都可以帮助我简单的testing和运行在Coffeescript使用Node.js?

提前致谢

你的例子对我来说运行良好。 在使用CoffeeScript支持之前,可能会使用旧版本的nodeunit; 尝试

 npm install -g nodeunit 

更新到最新版本。

如果失败了,那么我怀疑这是一个path问题,所以当nodeunit尝试require 'coffee-script' ,它会失败。

先做

 npm install -g coffee-script 

并注意输出的最后一行,应该看起来像这样

 coffee-script@1.1.2 /usr/local/lib/node_modules/coffee-script 

现在运行

 echo $NODE_PATH 

在我的情况下是/usr/local/lib/node_modules 。 您需要通过添加一行来将NODE_PATH设置为npm创build的coffee-script目录的父目录

 export NODE_PATH=/usr/local/lib/node_modules 

~/.profile或者~/.bashrc或者其他什么,你的shell在启动的时候运行,然后重启你的shell。 然后,只要机器上的任何Node应用程序require 'coffee-script' ,就会findCoffeeScript库。

如果您的咖啡脚本版本是1.7或更高版本,您可能还会遇到错误#247 ,我刚刚提交了一个补丁 。