[英]How to use the node.js module 'nodeunit' with coffeescript files
我正在嘗試讓nodeunit模塊在coffeescript項目中運行,但似乎無法運行基本測試。 這是我的例子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; ^ SyntaxError:在Function._load上的Module.load(module.js:334:31)處的Object..js(module.js:408:10)處的Module._compile(module.js:397:25)處的意外的令牌> (module.js:293:12)在/ usr / local / lib / node的/usr/local/lib/node/nodeunit/lib/nodeunit.js:75:37上的require(module.js:346:19) /nodeunit/deps/async.js:508:13 /usr/local/lib/node/nodeunit/deps/async.js:118:13 /usr/local/lib/node/nodeunit/deps/async.js :134:9 /usr/local/lib/node/nodeunit/deps/async.js:507:9
任何人都可以幫助我使用Node.js在Coffeescript中進行簡化測試並運行嗎?
提前致謝
你的例子適合我。 在你有CoffeeScript支持之前,你可能正在使用舊版本的nodeunit; 嘗試
npm install -g nodeunit
更新到最新版本。
如果失敗了,那么我懷疑這是一個路徑問題,所以當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創建的coffee-script
目錄的父目錄
export NODE_PATH=/usr/local/lib/node_modules
到~/.profile
或~/.bashrc
或它啟動時shell運行的其他任何東西,然后重新啟動shell。 然后,只要您在計算機上的任何Node應用程序中都require 'coffee-script'
,它就會找到CoffeeScript庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.