Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Node.js Basics 2017 Building a Command Line Application Making a GET Request with https

Roudy Antenor
Roudy Antenor
4,124 Points

What is the meaning of require() ?

Can someone please explain why we need to const http = require (http) - What is the meaning of this step? Why do we need to require anything? Explanations on the web are complicated to me

3 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

The thing you’re requiring requiring, http, is a module, or essentially a chunk of code someone else wrote that you’re going to use. In order to use it you have to pull it into your file. The require function does that for you. By storing the result of require in a variable (which you could call anything you want, but in this case also http), you can use that module more easily later in your code.

You’ll see this in a lot of programming languages. It’s sometimes called import.

Hope that makes sense!

Cheers :beers:

-Greg

Roudy Antenor
Roudy Antenor
4,124 Points

hi Greg, But it seems all we did is create a basic .js file - how does that file know what require () is? That file isn't linked to node.js - ( i didnt see that happen in the video) -did that file somehow become a NODE file that knows what const var = reguire(http) means -- in the videos the instructor just writes this command in the .js

Neil McPartlin
Neil McPartlin
14,662 Points

Hi Roudy. I note from your profile you have already done a lot on JavaScript and in the early stages we have been working with 'native' JavaScript which is being loaded into our browser. But with this particular course, we are now working with 'hosted' JavaScript i.e. Node.js. So think of app.js as the settings file and note how we always type 'node' before 'app.js' in order to run the program. Node 'is' the 'application' and node knows what to do when asked to invoke 'require'.

:clap::clap::clap::clap: very helpful

Routine Poutine
Routine Poutine
26,050 Points

If you can call the require variable anything, is this the only thing you will be requiring?

Import seems to have many different things you can import; is require only requiring https modules?