Tarang Hirani
Tarang Hirani

Reputation: 590

nodejs helloworld test app not running

I am new to learning node.js and I downloaded all the components on ubuntu. I have written a test, helloworld app, but it does not work for some reason. Here's my file layout.

When I execute which nodejs, the terminal returns /usr/bin/nodejs

When I execute which npm, the terminal returns /usr/bin/npm

My app.js test file is located in /home/tarang/node/helloWorld

Here's the source for the app.js

//creating http server
var http = require('require');

http.createServer(function(req, res){
    res.writeHead(200, {'Context-Type': 'text/plain'});
    res.end('helloWorld');
}).listen(8000);

console.log('server running');

UPDATE

When i execute nodejs app.js, i obtain the following error

Error: Cannot find module 'require'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/home/tarang/node/helloWorld/app.js:4:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)

Please do explain since I am new to ubuntu and nodejs.

Thanks!

UPDATE

Do i need to update to the lastest verions of express, node and npm?

Upvotes: 2

Views: 383

Answers (3)

Stephen Wright
Stephen Wright

Reputation: 2956

Issue is likely because you haven't installed the require module prior to running. You need to run npm install require on the command line before running the server, otherwise it won't be able to find the module.

Upvotes: 1

Chris Lam
Chris Lam

Reputation: 3614

the require module is missing according to the error message.

cd to the app root directory, run this:

npm install --save

Edit:

Hey wait, why are you require('require')?

I beleieve it should be require('http') instead

Upvotes: 1

Jacob Groundwater
Jacob Groundwater

Reputation: 6671

You want to import the http module, not require, so

//creating http server
var http = require('http');

Upvotes: 2

Related Questions