我正在从Courseera学习Node.js,并想在Windows 10的命令提示符下运行代码,但是此错误始终找不到模块,我完全不知所措。我已经尝试了其他堆栈溢出线程中给出的所有方法,但无法解决问题。分别使用v6.9.2和3.10.9版本正确安装了node和npm。
D:\shaury\node-http\public>node -v
v6.9.2
D:\shaury\node-http\public>npm -v
3.10.9
这是正在发生的问题:
D:\shaury\node-http\public>node serve
module.js:471
throw err;
^
Error: Cannot find module 'D:\shaury\node-http\public\serve'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
您的代码中有错别字。
var http = require('http');
var hostname = 'localhost';
var port = 3000;
var server = http.createServer(function(req, res){
console.log(req.headers);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<html><body><h1>Hello World</h1></body></html>');
});
//here you should surround your string in 'quotation marks' but make sure to use `backticks`
server.listen(port, hostname, function(){
console.log(`Server running at http://${hostname}:${port}/`);
});
编辑:实际上,您可能需要检查您是否在正确的目录中。启动应用程序时,它应来自.js文件所在的目录,否则应提供路径。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句