Error: Cannot find module './router'

4,152 views
Skip to first unread message

Kumar

unread,
Jul 13, 2011, 2:47:20 AM7/13/11
to nod...@googlegroups.com
I followed the steps given at http://howtonode.org/how-to-install-nodejs to install node.js on my Ubuntu machine. First few programs ran fine but after I tried to include the 'router' module, it started giving the following error:

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
Error: Cannot find module './router'
    at Function._resolveFilename (module.js:317:11)
    at Function._load (module.js:262:25)
    at require (module.js:346:19)
    at Object.<anonymous> (/home/kumar/dev/nodejs/helloworld/index.js:2:14)
    at Module._compile (module.js:402:26)
    at Object..js (module.js:408:10)
    at Module.load (module.js:334:31)
    at Function._load (module.js:293:12)
    at Array.<anonymous> (module.js:421:10)
    at EventEmitter._tickCallback (node.js:126:26)

Then I also tried to build and install by following the steps on https://github.com/joyent/node/wiki/Installation but the error isn't going away. Below are the results of 'make test' from the node build directory:

kumar@kumar-Lenovo-B460:~/dev/nodejs/node/node$ make test
Waf: Entering directory `/home/kumar/dev/nodejs/node/node/build'
DEST_OS: linux
DEST_CPU: ia32
Parallel Jobs: 1
Product type: program
Waf: Leaving directory `/home/kumar/dev/nodejs/node/node/build'
'build' finished successfully (0.097s)
python tools/test.py --mode=release simple message
=== release test-http-dns-fail ===                                            
Path: simple/test-http-dns-fail
assert.js:81
  throw new assert.AssertionError({
        ^
AssertionError: true == false
    at EventEmitter.<anonymous> (/home/kumar/dev/nodejs/node/node/test/simple/test-http-dns-fail.js:60:10)
    at EventEmitter.emit (events.js:81:20)
Command: build/default/node /home/kumar/dev/nodejs/node/node/test/simple/test-http-dns-fail.js
[00:45|% 100|+ 210|-   1]: Done                                           
make: *** [test] Error 1

Can someone please help me resolve it?

Marak Squires

unread,
Jul 14, 2011, 1:34:34 AM7/14/11
to nod...@googlegroups.com
User-land modules don't come shipped with node, you need to install them manually ( using a package manager like npm ).

I'm assuming "router" is the name of the module you want, try "npm install router". Check out https://github.com/joyent/node/wiki/modules and http://search.npmjs.org as well

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Kumar

unread,
Jul 15, 2011, 12:49:55 AM7/15/11
to nodejs
I tried
$ npm install router
rou...@0.3.0 ./node_modules/router
└── com...@0.1.1

then I tried
$ npm install biggie-router
biggie...@0.3.4 ./node_modules/biggie-router

But the error is still there

$ node index.js

node.js:134
throw e; // process.nextTick error, or 'error' event on first
tick
^
Error: Cannot find module './router'
at Function._resolveFilename (module.js:317:11)
at Function._load (module.js:262:25)
at require (module.js:346:19)
at Object.<anonymous> (/home/kumar/dev/nodejs/helloworld/index.js:
2:14)
at Module._compile (module.js:402:26)
at Object..js (module.js:408:10)
at Module.load (module.js:334:31)
at Function._load (module.js:293:12)
at Array.<anonymous> (module.js:421:10)
at EventEmitter._tickCallback (node.js:126:26)


On Jul 14, 1:34 am, Marak Squires <marak.squi...@gmail.com> wrote:
> User-land modules don't come shipped with node, you need to install them
> manually ( using a package manager like npm ).
>
> I'm assuming "router" is the name of the module you want, try "npm install
> router". Check outhttps://github.com/joyent/node/wiki/modulesandhttp://search.npmjs.orgas well
>
>
>
>
>
>
>
> On Tue, Jul 12, 2011 at 11:47 PM, Kumar <kuma...@gmail.com> wrote:
> > I followed the steps given athttp://howtonode.org/how-to-install-nodejstoinstall node.js on my Ubuntu machine. First few programs ran fine but
> > after I tried to include the 'router' module, it started giving the
> > following error:
>
> > node.js:134
> >         throw e; // process.nextTick error, or 'error' event on first tick
> >         ^
> > Error: Cannot find module './router'
> >     at Function._resolveFilename (module.js:317:11)
> >     at Function._load (module.js:262:25)
> >     at require (module.js:346:19)
> >     at Object.<anonymous> (/home/kumar/dev/nodejs/helloworld/index.js:2:14)
> >     at Module._compile (module.js:402:26)
> >     at Object..js (module.js:408:10)
> >     at Module.load (module.js:334:31)
> >     at Function._load (module.js:293:12)
> >     at Array.<anonymous> (module.js:421:10)
> >     at EventEmitter._tickCallback (node.js:126:26)
>
> > Then I also tried to build and install by following the steps on
> >https://github.com/joyent/node/wiki/Installationbut the error isn't going

Kumar

unread,
Jul 15, 2011, 1:06:27 AM7/15/11
to nodejs
Here are my server.js and index.js file contents

--- server.js ---
var http = require("http");
var url = require("url");

function start(route) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received");

route(pathname);

response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}

http.createServer(onRequest).listen(8888);
console.log("Server is started");
}

exports.start = start;

--- index.js ---
var router = require("./router");
var server = require("./server");

server.start(router.route);

Am I doing anything wrong here? I doubt.

On Jul 15, 9:49 am, Kumar <kuma...@gmail.com> wrote:
> I tried
> $ npm install router
> rou...@0.3.0 ./node_modules/router
> └── com...@0.1.1
>
> then I tried
> $ npm install biggie-router
> biggie-rou...@0.3.4 ./node_modules/biggie-router
>
> But the error is still there
>
> $ node index.js
>
> node.js:134
>         throw e; // process.nextTick error, or 'error' event on first
> tick
>         ^
> Error: Cannot find module './router'
>     at Function._resolveFilename (module.js:317:11)
>     at Function._load (module.js:262:25)
>     at require (module.js:346:19)
>     at Object.<anonymous> (/home/kumar/dev/nodejs/helloworld/index.js:
> 2:14)
>     at Module._compile (module.js:402:26)
>     at Object..js (module.js:408:10)
>     at Module.load (module.js:334:31)
>     at Function._load (module.js:293:12)
>     at Array.<anonymous> (module.js:421:10)
>     at EventEmitter._tickCallback (node.js:126:26)
>
> On Jul 14, 1:34 am, Marak Squires <marak.squi...@gmail.com> wrote:
>
>
>
>
>
>
>
> > User-land modules don't come shipped with node, you need to install them
> > manually ( using a package manager like npm ).
>
> > I'm assuming "router" is the name of the module you want, try "npm install
> > router". Check outhttps://github.com/joyent/node/wiki/modulesandhttp://search.npmjs.orgaswell
>
> > On Tue, Jul 12, 2011 at 11:47 PM, Kumar <kuma...@gmail.com> wrote:
> > > I followed the steps given athttp://howtonode.org/how-to-install-nodejstoinstallnode.js on my Ubuntu machine. First few programs ran fine but
> > > after I tried to include the 'router' module, it started giving the
> > > following error:
>
> > > node.js:134
> > >         throw e; // process.nextTick error, or 'error' event on first tick
> > >         ^
> > > Error: Cannot find module './router'
> > >     at Function._resolveFilename (module.js:317:11)
> > >     at Function._load (module.js:262:25)
> > >     at require (module.js:346:19)
> > >     at Object.<anonymous> (/home/kumar/dev/nodejs/helloworld/index.js:2:14)
> > >     at Module._compile (module.js:402:26)
> > >     at Object..js (module.js:408:10)
> > >     at Module.load (module.js:334:31)
> > >     at Function._load (module.js:293:12)
> > >     at Array.<anonymous> (module.js:421:10)
> > >     at EventEmitter._tickCallback (node.js:126:26)
>
> > > Then I also tried to build and install by following the steps on
> > >https://github.com/joyent/node/wiki/Installationbutthe error isn't going

Martin Cooper

unread,
Jul 15, 2011, 1:13:57 AM7/15/11
to nod...@googlegroups.com
On Thu, Jul 14, 2011 at 10:06 PM, Kumar <kum...@gmail.com> wrote:
> Here are my server.js and index.js file contents
>
> --- server.js ---
> var http = require("http");
> var url = require("url");
>
> function start(route) {
>        function onRequest(request, response) {
>                var pathname = url.parse(request.url).pathname;
>                console.log("Request for " + pathname + " received");
>
>                route(pathname);
>
>                response.writeHead(200, {"Content-Type": "text/plain"});
>                response.write("Hello World");
>                response.end();
>        }
>
>        http.createServer(onRequest).listen(8888);
>        console.log("Server is started");
> }
>
> exports.start = start;
>
> --- index.js ---
> var router = require("./router");
> var server = require("./server");
>
> server.start(router.route);
>
> Am I doing anything wrong here? I doubt.

You'd use require("./router") for a project-local router.js file - one
that is sitting beside your index.js and server.js files. You'd use
require("router") for an installed dependency.

It seems that you expect 'router' to be coming from an installed
dependency, since you mention "npm install router", but your code is
expecting a project-local router.js file, which I assume you don't
have.

--
Martin Cooper

Kumar

unread,
Jul 15, 2011, 1:22:21 AM7/15/11
to nodejs
Thanks a lot Martin! Saved my day, it was getting frustrated.

I just started learning node.js from the book "The Node Beginner
Guide" by Manuel Kiessling. The mistake I did is coming straight from
the book.

Thanks for the explanation, again.

Kumar

unread,
Jul 15, 2011, 2:52:44 AM7/15/11
to nodejs
I completely missed one step in the book: creating a file router.js
with following contents:

function route(pathname) {
console.log("About to route a request for " + pathname);
}
exports.route = route;

Shucks!! Stupid me!!
Reply all
Reply to author
Forward
0 new messages