不能需要本地 CoffeeScript 模块

2024-05-06

我正在运行 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)

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

解决方案是:

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

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

 require('coffee-script/register');

对于 CoffeeScript 1.7 来说,发生的事情是,引入了重大更改 https://github.com/jashkenas/coffee-script/pull/3279.

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

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

不能需要本地 CoffeeScript 模块 的相关文章

随机推荐