I have a fundamental query of how the require()
works in nodeJS application.
What does require()
actually return ??
For example, say I wanted to use 2 third party packages : lodash
and request
. After installing the package, We can write the code as :
const _ = require('lodash')
_.uniq(Array)
So shall I understand that require()
return an object here and uniq
is a method from the object _
?
Now let's consider request
package,
const request = require('request')
request (
{
url: '',
json:true
}
callback_function()
)
So shall I understand here that require()
had returned a method directly - request()
If I use the same as below, I am encountering a type error that req1.request is not a method
.
const req1 = require('request');
req1.request (
{
url: '',
json:true
}
callback_function()
)
Kindly help me in understanding this.