51

As per the node js instruction manual, it is highly discouraged to install any node module globally.

I tried a variety of things. I executed the following commands in my home directory, in my git file (with the node_modules) folder, and the actual node_modules folder.

> var express=require('express');
undefined
> var express=require('node_modules/express');
Error: Cannot find module 'node_modules/express'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at repl:1:13
    at REPLServer.self.eval (repl.js:110:21)
    at repl.js:249:20
    at REPLServer.self.eval (repl.js:122:7)
    at Interface.<anonymous> (repl.js:239:12)
    at Interface.EventEmitter.emit (events.js:95:17)

Note that in the above, I expected them in my git folder (with node_modules). I found it curious that the line

> var express=require('express');

yielded

undefined

In the home directory, I just get the same "cannot find module" error.

autistic
  • 1
  • 3
  • 35
  • 80
user2316667
  • 5,444
  • 13
  • 49
  • 71

3 Answers3

58

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
Community
  • 1
  • 1
Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • Thanks! Why does it print undefined though? – user2316667 Aug 15 '13 at 19:57
  • 2
    And here for an ECMA-262 discussion: [assigment in javascript and the var keyword](http://stackoverflow.com/questions/13180195/assigment-in-javascript-and-the-var-keyword/13180929#13180929) – dc5 Aug 15 '13 at 20:08
  • undefined is the result of a variable assignment, there's nothing interesting about this, it's just how it works. If you typed JUST require(''), you would get the value. – Sprague Mar 13 '15 at 13:25
  • I get the above exception in the REPL for all modules but those that came with node.js. My modules are locally installed, and ``require`` does work for these modules from within a script handed to the node executable. – Joe Lapp Jun 05 '16 at 19:50
  • I solved my problem. require() was broken in the v5.2.0 REPL, which is the version I was using. https://github.com/nodejs/node/issues/4208 – Joe Lapp Jun 05 '16 at 20:09
  • Haven't looked into why but be careful when requiring lodash/underscore as the doing `var _ = require('lodash');` will still be undefined, but assigning it to anything other than an underscore, like `var foobar = require('lodash');`, will work (for me atleast): – DrunkenBeetle Jun 07 '16 at 04:16
4

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  
dc5
  • 12,341
  • 2
  • 35
  • 47
  • I appreciate it. I tried express. (pressed tab key) but got 3 dots. But 'express' yields a giant list of stuff. – user2316667 Aug 15 '13 at 20:03
  • Interesting - what version of node & OS? – dc5 Aug 15 '13 at 20:06
  • Perhaps this will help: [Node JS REPL, Sockets, and Telnet - Tab Completion, Arrow Keys, etc](http://stackoverflow.com/questions/8649861/node-js-repl-sockets-and-telnet-tab-completion-arrow-keys-etc). When working in the repl tab completion is nice to have available. – dc5 Aug 15 '13 at 20:25
  • or this: [Bash tab-auto complete not working 100%](http://ubuntuforums.org/showthread.php?t=1865538) – dc5 Aug 15 '13 at 20:52
4

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.

user3738936
  • 936
  • 8
  • 22