使用 Ubuntu 的第二天:安装了 NodeJS,运行了我的第一个 NodeJS 示例。控制台中出现此错误:
/home/privateuser/nodejsweb/helloweb.js:11
listen(9999);
^
ReferenceError: listen is not defined
at Object.<anonymous> (/home/privateuser/nodejsweb/helloweb.js:11:5)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:929:3
代码:
var http = require("http");
http.createServer(function(request,response)
{
response.writeHead(200, {"Content-Type": "text/html"});
response.write("<html>");
response.write("<head><title>Node.JS</title></head>");
respons.write("<body>Hello web</body>");
respons.write("</html>");
response.end();
}),
listen(9999);
然后我记得在安装 NodeJS 期间一些可选功能不起作用:
$ apt-get install -y build-essential
E: Kon het vergrendelingsbestand '/var/lib/dpkg/lock' niet openen - open (13: Toegang geweigerd)
E: Kan de beheersmap (/var/lib/dpkg/) niet vergrendelen. Heeft u beheerdersrechten?
英文:Could not lock directory
并询问管理员权限。
我根据这个建议安装了 NodeJS:在 Ubuntu 的软件中心找不到 NodeJS
尝试查看哪个程序正在锁定目录:
$ sudo lsof /var/lib/dpkg/loc
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
Output information may be incomplete.
lsof: status error on /var/lib/dpkg/loc: No such file or directory
lsof 4.86
latest revision: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/
latest FAQ: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/FAQ
latest man page: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/lsof_man
usage: [-?abhKlnNoOPRtUvVX] [+|-c c] [+|-d s] [+D D] [+|-f[gG]] [+|-e s]
[-F [f]] [-g [s]] [-i [i]] [+|-L [l]] [+m [m]] [+|-M] [-o [o]] [-p s]
[+|-r [t]] [-s [p:s]] [-S [t]] [-T [t]] [-u s] [+|-w] [-x [fl]] [--] [names]
Use the ``-h'' option to get more help information.
答案1
来自 Stack Overflow:
listen 是一个需要在 createServer 返回的内容上调用的函数
因此,listen 之前的逗号必须用点代替。
其次,由于两个拼写错误,出现了新的错误:respons
应该是response
:)