TruePS
TruePS

Reputation: 523

Unable to run my first node.js program?

I opened a text file on desktop.In that file I wrote

var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

Then I saved the file with name example.js and when i type node example.js this error gets printed

 Error: Cannot find module 'C:\Users\admin\Desktop\example.js'
 at Function.Module._resolveFilename (module.js:338:15)
 at Function.Module._load (module.js:280:25)
 at Function.Module.runMain (module.js:497:10)
 at startup (node.js:119:16)
 at node.js:902:3

What am I doing wrong here the file example.js is on Desktop

Upvotes: 0

Views: 2471

Answers (3)

Fresher
Fresher

Reputation: 892

Please firstly check node js is installed or not.open a terminal window and type node -v. Also you should check nodejs file permissions.You can use this link for that.If problem persists kindly try to reinstall nodejs.You should try to install nodejs globally to run your program from anywhere.

Upvotes: 0

Diana Nassar
Diana Nassar

Reputation: 2303

I created a file named example.js on my desktop, ran it with node example.js and it worked. Then I created the same file but its type was text example.js.txt - I got your very same result as shown below:

enter image description here

Is this the case?

Upvotes: 3

Tushar Sonawane
Tushar Sonawane

Reputation: 301

@TruePS Please verify that the file you have created has .js extension, as pointed out by @kelz

I guess your file has .txt extension, as you are saying "I opened a text file on desktop"

Try node example.txt if it is .txt. (as workaround)

Function.Module._resolveFilename (module.js:338:15) occurs when filename doesn't match.

Upvotes: 0

Related Questions