【问题标题】:Node.js require() returns nothingNode.js require() 什么都不返回
【发布时间】:2016-03-16 10:02:31
【问题描述】:

我在 Windows 8 上使用带有 Node.js 的 CoffeeScript。我有一个包含 2 个文件的目录,main.coffee 和 test.coffee。 main.coffee 中的代码是:

test = require "./test.coffee"
console.log test.x * 2

test.coffee 的代码是:

x = 2

当我运行coffee main.coffee -n 时,我会返回NaN。我尝试在 requireconsole.log 上使用括号,但它并没有改变任何东西。

【问题讨论】:

  • 您需要从您的 test.coffee 文件中设置导出值。例如。 module.exports = { x: 2 } 将是 test.x //2

标签: node.js coffeescript require


【解决方案1】:

Node.js 要求您的模块使用exports 来定义从require 导出的内容

试试 test.coffee of:

exports.x = 2;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-18
    • 2018-09-10
    • 2017-02-14
    • 2013-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多