user2316667 user2316667 - 5 months ago 33
Node.js Question

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

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');
> 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');



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


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
> var express = require('express');
> express
//long object which is the express module gets printed