不能需要本地 CoffeeScript 模块 [英] Can't require local CoffeeScript modules
问题描述
I'm running Node.js 0.10.21. I tried both CoffeeScript 1.6.3 and master both with and without require('coffee-script/extensions')
. Compiling the two files to JavaScript and running them directly in Node works just fine of course.
# ./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)
I found this SO question while trying to solve this problem for CoffeeScript version 1.7.1. It doesn't apply to the OP's version 1.6.3 but it may help others with this problem in 2014 and later.
The solution is to either:
var foo = require('coffee-script/register');
foo.register();
or, you can simply do this (which is my usual preference):
require('coffee-script/register');
What's happening is that for CoffeeScript 1.7, a breaking change was introduced.
It solves for cases where a variety of coffee-script versions are used within the set of dependencies that you may be loading or that your dependencies are loading.
The idea is that any particular module (or sub-module) should be able to be compiled by the version of coffee-script that it is compatible with.
Read about that here: https://github.com/jashkenas/coffee-script/pull/3279.
这篇关于不能需要本地 CoffeeScript 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!