Search code examples
node.jsnpmsudo

request nodejs package not being found


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


Solution

  • 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": "*"
      }
    }