Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Require() in REPL does not seem to work properly

Require() in REPL does not seem to work properly

Problem

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
Problem courtesy of: Nyxynyx

Solution

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.

Solution courtesy of: Farid Nouri Neshat

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Require() in REPL does not seem to work properly

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×