【问题标题】:Require() in REPL does not seem to work properlyREPL 中的 Require() 似乎无法正常工作
【发布时间】:2011-11-22 13:27:07
【问题描述】:

问题:我刚开始在 node.js 中使用 REPL 到 require 一个模块时,它的功能一直显示未定义。哪里出错了?

另外,为什么var s = require('./simple'); 行会导致undefined 响应?我正在使用节点 v0.6.2

simple.js

var counts = 0;
exports.next = function() { counts++; }

我在 REPL 中做了什么

> var s = require('./simple');
undefined
> s.next
[Function]
> s.next()
undefined
> s.next();
undefined

【问题讨论】:

  • 您是否在与文件相同的目录中启动 REPL?
  • @pradeek 是的,我在与文件simple.js 相同的目录中输入了node

标签: javascript node.js express read-eval-print-loop


【解决方案1】:

这是完全正常的,因为你的函数实际上并没有返回任何它会默认返回 undefined 的东西。试试这个exports.next = function() {return counts++; } 你会得到加法前的号码。

【讨论】:

    猜你喜欢
    • 2017-09-04
    • 2012-05-06
    • 2013-02-17
    • 2017-10-09
    • 2017-08-03
    • 2014-05-02
    • 2011-09-11
    • 2016-02-11
    • 1970-01-01
    相关资源
    最近更新 更多