my favorite

2010/04/19

nodejs : require lib but remember DO NOT add an extension

I follow some example wrote on somebody's(!@$!@$#@!$) blog and I had such issue :


Error: Cannot find module '/sys'
at loadModule (node.js:492:15)
at require (node.js:618:12)
at Object. (/Users/huge/test:3:11)
at Module._compile (node.js:639:23)
at node.js:667:20
at fs:52:23
at node.js:748:9


#!/usr/local/bin/node

var sys = require("/sys.js"), http = require("/http.js");
http.createServer(function (req, res){
        setTimeout(function() {
                res.sendHeader(200, {"Content-Type": "text/plain"});
                res.sendBody("Hello XD");
                res.finish();
        }, 2000);
}).listen(8000);

sys.puts("Server running at http://localhost:8000");

This error raised because I try to add extension while requiring a library.
Shortly speaking,
require("sys") is correct
require("sys.js") or require("/sys.js") is incorrect

Time passed and code changed :P, please use example wrote below for your testing
#!/usr/local/bin/node

var sys = require("sys"), 
    http = require("http");

http.createServer(function (req, res){
        setTimeout(function() {
                res.writeHead(200, {"Content-Type": "text/plain"});
                res.end("Hello World\n");
        }, 2000);
}).listen(8000);

Thanks for friends from irc node.js channel :)

No comments:

BIO

Taipei, GuTing, Taiwan

huang47 | personal

huang47 | personal