JavaScript Node.js Basics 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
38,993 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,636 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'.

Routine Poutine
Routine Poutine
26,034 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?