How To Require Node Module In Node REPL Without Installing Globally?

Solution 1:

Your require statement is working fine. Ignore the undefined, that's just the node REPL. The undefined is explained here, and see the comments below for links to additional material about that.

You can verify with:

mkdir /tmp/test-repl
cd /tmp/test-repl
npm install express
node
> var express = require('express');
undefined
> express
//long object which is the express module gets printed

Solution 2:

Printing undefined is normal behavior for both the browser console and the node repl.

Try typing: express. (tab key) - you should get something like this:

> var express = require('express');
undefined
> express.
express.__defineGetter__      express.__defineSetter__      express.__lookupGetter__      express.__lookupSetter__      express.constructor           express.hasOwnProperty
express.isPrototypeOf         express.propertyIsEnumerable  express.toLocaleString        express.toString              express.valueOf               

express.apply                 express.arguments             express.bind                  express.call                  express.caller                express.constructor
express.length                express.name                  express.toString              

express.Route                 express.Router                express.application           express.arguments             express.basicAuth             express.bodyParser
express.caller                express.compress              express.cookieParser          express.cookieSession         express.createServer          express.csrf
express.directory             express.errorHandler          express.favicon               express.json                  express.length                express.limit
express.logger                express.methodOverride        express.mime                  express.multipart             express.name                  express.prototype
express.query                 express.request               express.response              express.responseTime          express.session               express.static
express.staticCache           express.timeout               express.urlencoded            express.version               express.vhost  

Solution 3:

Depending on the terminal/shell it may want you to specify the current directory. I am using gitbash at the moment.

 _u = require('./node_modules/underscore/underscore');

When I do this the object is returned.

node repl require example

I am curious if this works for others, it worked for me.