2

I install request package using npm. It appears to be located here: /usr/local/lib/node_modules/request/

var request = require("request");

request("http://www.google.com", function(error, response, body) {
  console.log(body);
});

module.js:340 throw err; ^ Error: Cannot find module 'request'...

What do I need to alter or perform? Further info, MAC OSX, node-v0.10.26.pkg, sudo -H npm install -g request, no errors

Paul
  • 608
  • 1
  • 9
  • 23
  • What command did you use to install the package? Were you in the same directory as your node app? – Josh C. Mar 10 '14 at 14:21
  • [npm and node without sudo](http://stackoverflow.com/questions/10081293/install-npm-into-home-directory-with-distribution-nodejs-package-ubuntu/13021677#13021677) – Matthew Bakaitis Mar 10 '14 at 17:06

3 Answers3

6

I think you have installed the package using the -g flag (global).
That's not how you should have installed the package.

To fix your problem, install the package locally:

npm install request

Or use a package.json file to persist the dependency:

{
  "name": "test",
  "version": "0.1.0",
  "dependencies": {
    "request": "*"
  }
}
Community
  • 1
  • 1
Florent
  • 12,310
  • 10
  • 49
  • 58
  • I found this advice (sudo -H npm install -g) due to the fact that npm was root. npm install request will not work. I'll look at package.json (I am new to nodejs). – Paul Mar 10 '14 at 14:59
2

You need to install the package in your project directory, not globally. So run npm install from your project directory without the -g flag.

Koen
  • 545
  • 3
  • 12
0

Use advice by @Florent. Your direct problem, however, is that the global location is not known to node. If you still want to enable it, set the value of NODE_PATH to /usr/local/lib/node_modules (on Mac OS X in /etc/launchd.conf to make it available to all on boot).

Oleg Sklyar
  • 9,834
  • 6
  • 39
  • 62
  • This is how I install `nodeunit` for unit testing as I do not want to have it as a module dependency. Works like a charm. – Oleg Sklyar Mar 10 '14 at 15:04