簡體   English   中英

NodeJS入門 - 無法在nodeJS中找到模塊

[英]Getting started with NodeJS - Cannot find module in nodeJS

我是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.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

您應該編寫節點app.js而不是節點apjs

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM