Javascript NodeJS入门-在NodeJS中找不到模块
我是NodeJS新手,我正试图根据NodeJS站点上的指南在NodeJS中创建一个服务器。我已经在我的计算机上安装了NodeJS,并用下面的代码生成app.js文件Javascript NodeJS入门-在NodeJS中找不到模块,javascript,node.js,npm,Javascript,Node.js,Npm,我是NodeJS新手,我正试图根据NodeJS站点上的指南在NodeJS中创建一个服务器。我已经在我的计算机上安装了NodeJS,并用下面的代码生成app.js文件 const http = require('http'); const hostname = '127.0.0.1'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setH
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
然后,我尝试通过在cmd(Windows 10)中键入node app.js
命令来运行代码。之后显示以下错误
C:\Users\Nuwanst\Documents\NodeJS\test2>node appjs
module.js:471
throw err;
^
Error: Cannot find module 'C:\Users\Nuwanst\Documents\NodeJS\test2\appjs'
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:389:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:504:3
如何修复此问题?根据错误,我认为您在cmd中运行node.js时出现了错误。
应该是
node app.js
而不是node appjs
您应该编写node app.js而不是node apjs