不能要求本地CoffeeScript模块

sup*_*kas 3 node.js coffeescript

我正在运行Node.js 0.10.21.我尝试了两种CoffeeScript 1.6.3,无论是否使用require('coffee-script/extensions').将这两个文件编译为JavaScript并直接在Node中运行它们当然可以正常工作.

# ./folder/a.coffee
require('../b').test()

# ./b.coffee
exports.test = -> console.log 'yay'

# $ coffee folder/a.coffee
#
# Error: Cannot find module '../b'
#   at Function.Module._resolveFilename (module.js:338:15)
#   at Function.Module._load (module.js:280:25)
#   at Module.require (module.js:364:17)
#   at require (module.js:380:17)
#   at Object.<anonymous> (/Users/test/folder/a.coffee:1:1)
#   at Module._compile (module.js:456:26)
Run Code Online (Sandbox Code Playgroud)

Ste*_*son 13

我试图解决CoffeeScript 版本1.7.1的这个问题时发现了这个问题.它不适用于OP的1.6.3版本,但它可能在2014年及以后帮助其他人解决这个问题.

解决方案是:

 var foo = require('coffee-script/register');
 foo.register();
Run Code Online (Sandbox Code Playgroud)

或者,你可以简单地这样做(这是我通常的偏好):

 require('coffee-script/register');
Run Code Online (Sandbox Code Playgroud)

正在发生的事情是,对于CoffeeScript 1.7,引入了一个重大变化.

它解决了在您可能正在加载的依赖关系集中或者正在加载依赖关系的情况下使用各种咖啡脚本版本的情况.

这个想法是任何特定的模块(或子模块)都应该能够与它兼容的咖啡脚本版本进行编译.

在这里阅读:https://github.com/jashkenas/coffee-script/pull/3279.