Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

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.

like image 629
user2316667 Avatar asked Aug 15 '13 19:08

user2316667


People also ask

How do I require a node module?

You can think of the require module as the command and the module module as the organizer of all required modules. Requiring a module in Node isn't that complicated of a concept. const config = require('/path/to/file'); The main object exported by the require module is a function (as used in the above example).

DO built-in modules in NodeJS need to be installed?

js Built-in Modules. Node. js has a set of built-in modules which you can use without any further installation.


2 Answers

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
like image 199
Peter Lyons Avatar answered Oct 18 '22 19:10

Peter Lyons


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  
like image 31
dc5 Avatar answered Oct 18 '22 17:10

dc5