Nwn
Nwn

Reputation: 571

Getting started with NodeJS - Cannot find module in nodeJS

I'm new to NodeJS and im trying to create a server in NodeJS accouding to the Guide on the NodeJS site. I already installed NodeJS to my computer and make app.js file with following code.

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}/`);
});

then I tried to run the code by typing node app.js command in the cmd(Windows 10). After that it shows following error.

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

how to fix this?

Upvotes: 4

Views: 559

Answers (2)

Anuj Kumar
Anuj Kumar

Reputation: 17

You should be writing node app.js instead of node apjs

Upvotes: 0

Eunice Poh
Eunice Poh

Reputation: 528

According to the error, I think you have got a typo when running your node.js in the cmd. It should be node app.js and not node appjs

Upvotes: 3

Related Questions