nodejs : require lib but remember DO NOT add an extension
四月 19, 2010 Posted in RSS 觀看文章來源
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.
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
讀過本文的讀者, 也對以下文章有興趣
Working Syphon: 光影魔術手























