Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Node.js Basics (2014) Building a Command Line Application Handling Errors in Node

John Lukacs
John Lukacs
26,210 Points

Require(http) Undefined?

Do any off you have any experiance with node.js. Why in the console am I getting this from the web site I made. what exactly is require(http); requireing

7 Answers

Seth Kroger
Seth Kroger
56,406 Points

require() loads in a module for use in your code. The result of require should be stored in a variable so it can be referenced for use later. The argument for require is supposed to be a string, almost always a string literal. require(http) means require what is in the variable http, which is probably undefined at that point. It should be:

const http = require('http');

This way you can use the functionality of node's http module through http in your code. HTTP is the protocol for making and responding to website requests.

Lucas Santos
Lucas Santos
19,315 Points

You probably did not install the Node Module for HTTP. Make sure the module is installed via npm.

You should be able to see it in your node_modules folder.

Also as Seth mentioned above you want to assign it to a variable if you haven't done so already.

Seth Kroger
Seth Kroger
56,406 Points

http is one of node's core modules. It's installed with node and globally available.

John Lukacs
John Lukacs
26,210 Points

Thanks for taking the time. Yeah I thought about the fact that I did not have node installed. I recently downloaded but I

am still getting the error not defined in the console Also I have set it to a variable and set it to the string literal as the

video instructed.

I don't have a node folder from the download it asked me if I wanted to trash it and I agreed because I did not think i needed it. Is there a video on how to install the module from npm node package manager.

In bash I run the code and for status code console.log(response.statusCode); I get back a 301 error. I will look up how to install HTTP module in the meantime

Seth Kroger
Seth Kroger
56,406 Points

301 is a redirect response, meaning the resource is at another location. If you having trouble with the course project replace http with https as there was a website change since the course was first put out. This is covered in the teacher's notes of a prior video where you first try to get the profile.

John Lukacs
John Lukacs
26,210 Points

I ran this line npm install http-server -g persmission denied It took a while and output alot of lines

John Lukacs
John Lukacs
26,210 Points

I have 21 folders with the name node_modules what am I looking for

John Lukacs
John Lukacs
26,210 Points

HI Seth when I try this in chrome it does not work I get in console Uncaught ReferenceError: require is not defined When I try it in bash it works fine. DO I need to download a extension for chrome as I am not using workspaces

Seth Kroger
Seth Kroger
56,406 Points

Running Node script inside of Chrome isn't going to work because even though basic language is the same, the environments you're working with are different. Running node outside of workspaces involves downloading and installing node from nodejs.org and running it through a terminal or command prompt.

John Lukacs
John Lukacs
26,210 Points

Hi Seth thanks for taking the time. Yes I ran it threw terminal and it worked. Now Im stuck on how to document.write the answer I got to the page. Is document.write the only way to show my javascript points and badges or showed I just look for the answer in the next node video