不能要求本地CoffeeScript模块 [英] Can't require local CoffeeScript modules

查看:202
本文介绍了不能要求本地CoffeeScript模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行Node.js 0.10.21。我尝试了两个CoffeeScript 1.6.3和master有和没有 require('coffee-script / extensions')。将这两个文件编译为JavaScript并在Node中直接运行它们是很好的。

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)


推荐答案

我在尝试解决CoffeeScript 版本1.7.1 时遇到此问题。它不适用于OP的1.6.3版本,但它可能会帮助其他人在2014年及以后的这个问题。

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.

解决方案是:

 var foo = require('coffee-script/register');
 foo.register();

或者,您可以这样做(这是我通常的偏好):

or, you can simply do this (which is my usual preference):

 require('coffee-script/register');

发生的是,对于CoffeeScript 1.7,引入了一个破坏性的更改

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.

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

这篇关于不能要求本地CoffeeScript模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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