Search code examples
node.jsmodulefindnode.js-connect

Cannot find module 'connect' on Mac OS X Lion


I installed the Connect module using NPM running the following command:

npm install connect

It created the module in the /Download/usr/node_modules/connect folder. I created a file which uses the Connect module using:

var connect = require('connect');
var util = require('util');

function sendjson(res,obj)
{
    res.writeHead(200,{'Content-Type':'application/json',});

    var objstr = JSON.stringify(obj);
    util.debug('SENDJSON' + objstr);
    res.end(objstr);
} 

var server = connect.createServer(
    connect.router(function(app){

        app.get('/foo', function(req,res){
            sendjson(res,{path:'foo'});
        })
        app.get('/bar', function(req,res){
            sendjson(res,{path:'bar'});
        })
    })
);

 server.listen(3000);

I run node createServer.js and it throws in the terminal and it gives me the following error.

Cannot find module 'connect'


Solution

  • NPM modules by default need to be installed locally in the folder that contains the source file that uses them. So if your source file is in /Desktop/nodescripts, you should run "npm install connect" in that same folder. That will create node_modules folder in that path, and your script will be able to find it.