Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS | module.js:540 throw err

So it's the first ever program I write but when I run it in the console I get this error.

module.js:540
    throw err;
    ^

Error: Cannot find module 'C:\Users\Daniel\Desktop\app'
    at Function.Module._resolveFilename (module.js:538:15)
    at Function.Module._load (module.js:468:25)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3

I have no idea why this is happening as I am new but I checked the code and nothing is wrong.

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!');
});

server.listen(port, hostname, () => {
    console.log('Server started on port '+port);
});
like image 729
John Smith Avatar asked Jan 09 '18 12:01

John Smith


People also ask

What is throw error in Nodejs?

The throw statement throws a user-defined exception. Execution of the current function will stop (the statements after throw won't be executed), and control will be passed to the first catch block in the call stack. If no catch block exists among caller functions, the program will terminate.

What is uncaught exception in node js?

The 'uncaughtException' event is emitted when an uncaught JavaScript exception bubbles all the way back to the event loop. By default, Node. js handles such exceptions by printing the stack trace to stderr and exiting with code 1, overriding any previously set process.

What is the preferred method of resolving unhandled exceptions in node js?

Approach 1: Using try-catch block: We know that Node. js is a platform built on JavaScript runtime for easily building fast and scalable network applications. Being part of JavaScript, we know that the most prominent way to handle the exception is we can have try and catch block.


1 Answers

It seems like the script you wanted to execute isn't named "app".

Check the Path and name of your script when you execute it with the node command.

like image 74
NullDev Avatar answered Sep 28 '22 06:09

NullDev