REPL 中的 Require() 似乎无法正常工作
问题:我刚刚开始使用 Node.js,当使用 REPL 来 require
模块时,其函数始终显示未定义。哪里出了问题?
另外,为什么 var s = require('./simple');
行会导致 undefined
响应?我正在使用 Node 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
Problem: I just started out in node.js and when using REPL to require
a module, its function keeps showing undefined. Where did it go wrong?
Also, why does the var s = require('./simple');
line result in an undefined
response? I am using node v0.6.2
simple.js
var counts = 0;
exports.next = function() { counts++; }
What I did in REPL
> var s = require('./simple');
undefined
> s.next
[Function]
> s.next()
undefined
> s.next();
undefined
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是完全正常的,因为您的函数实际上不会返回任何内容,默认情况下它会返回未定义的内容。试试这个
exports.next = function() {return counts++; }
你得到了加法之前的数字。That is completely normal, since your function doesn't actually return anything it would return undefined by default. Try this
exports.next = function() {return counts++; }
you get the number before the addition.