2

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?

1
  • Just updated it. Please have a look! Commented Jul 13, 2014 at 18:17

3 Answers 3

2

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

//creating http server
var http = require('http');
Sign up to request clarification or add additional context in comments.

6 Comments

Also, why do some websites say I need to use node app.js to execute it, while I have to use nodejs app.js to execute it? Is this a version issues?
The Ubuntu node package is non-standard, and changes the executable name to nodejs. It should be node but you'll have to take that up with package maintainers.
So I guess, if it works, it works? I don't need to be worried about it, right?
Yah, especially if you're just getting started. Later you can check out using a ppa. It's usually a little more up to date.
But it does say that I have the lastest version of nodejs. nodejs --version returns v0.10.25
|
1

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

5 Comments

So cd to the location where node was installed i.e. usr/bin/nodejs ?
nope, the root direectory of your app, which should be /home/tarang/node/helloWorld I believe.
the terminal returns "could not read dependencies". Why is this so complicateD?
so i beleieve you dont have a package.json yet. see my answer after a few mins while i am editing :D
I don't think I have a package.json yet. I am following this tutorial. code.tutsplus.com/tutorials/…
1

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.

8 Comments

I just did that, and it returned the same error. I am not sure what is going on.
You can also install it globally: npm install -g require
confirm that you now have a node_modules directory in the same directory as your app.js?
How do i do that? I'm really confused. I assume the error is because node cannot find some of the necessary files
ls -ltr on the ubuntu command line ...that being the case, i'm not entirely sure why you're using require in the first place? If you're just trying to replicate the Node basic tutorial, you should use the http module, not the require module.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.