Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js ReferenceError

Tags:

This is my first go at NodeJS. I've installed it successfully on an instance at DigitalOcean.

I have the following helloworld.js

require("http");  http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World"); response.end(); }).listen(8888);console.log('Hello world'); 

When I run it via "node helloworld.js", I get the following error:

/home/jason/helloworld.js:4 http.createServer(function(request, response) { ^ ReferenceError: http is not defined at Object.<anonymous> (/home/jason/helloworld.js:4:1) 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) at startup (node.js:119:16) at node.js:901:3 jason@do:~$ 

Can someone point me in the right direction?

like image 425
JasonStockman Avatar asked Jul 07 '13 02:07

JasonStockman


People also ask

What is ReferenceError in node JS?

The ReferenceError object represents an error when a variable that doesn't exist (or hasn't yet been initialized) in the current scope is referenced.

How do I fix uncaught ReferenceError?

Answer: Execute Code after jQuery Library has Loaded The most common reason behind the error "Uncaught ReferenceError: $ is not defined" is executing the jQuery code before the jQuery library file has loaded. Therefore make sure that you're executing the jQuery code only after jQuery library file has finished loading.

How do I fix uncaught ReferenceError is not defined in jQuery?

To solve this error, we simply add the jQuery CDN link or downloaded jQuery path link inside the head section. Example: This example resolves the error by adding the required CDN link inside the <script> tag.


1 Answers

require() doesn't work like #include or import does in other languages.

require() returns a reference to the resolved module. That reference must be assigned to a variable.

var http = require('http'); //the variable doesn't necessarily have to be named http http.createServer(function(req, res) {}); 

Or

require('http').createServer(function(req, res) { }); 
like image 197
c.P.u1 Avatar answered Oct 20 '22 05:10

c.P.u1