2

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.

Kunj
  • 1,980
  • 2
  • 22
  • 34
METALHEAD
  • 2,734
  • 3
  • 22
  • 37
  • 1
    **1** *So shall I understand that `require()` return an object here and `uniq` is a method from the object* yes **2**. *shall I understand here that `require()` had returned a method directly - `request()`*. No. Here, `request` could've been `bottle`. It's just a variable name. But **it holds whatever was exported** through `module.exports` of the referenced package – Adelin Jun 18 '18 at 06:52
  • Hi@Adelin.. Thank you for the explanation. I've gone through the `uniq.js` and `request.js` from the installed packages. I've found the code present there as - `module.exports = uniq;` and `module.exports = Request`. And nowhere they are exporting any objects. Can you please explain why we used `_.uniq()` instead of direct `uniq()`. Thank you – METALHEAD Jun 18 '18 at 07:13

1 Answers1

3

require returns whatever the module defined. Sometimes, the module defines a single function:

exports = function request(/*...*/);

...and so when you import the module, that's what you get.

Sometimes, modules export objects (exports refers to a blank object initially when the module is loaded, and then modules add to or replace that object):

exports._ = {
    // ...
};

...and so when you import the module, that's what you get.

It's up to the module. It's a very simple system. More in the Modules documentation (not to be confused with the ECMAScript (JavaScript) Modules documentation; JavaScript's own modules work slightly differently from Node.js's).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Hi Crowder, thank you so much for the detailed explanation. I've gone through the `uniq.js` and `request.js` from the installed packages. I've found the code present there as - `module.exports = uniq;` and `module.exports = Request`. And no where they are exporting any objects. Can you please explain why we used `_.uniq()` instead of direct `uniq()`. Thank you. – METALHEAD Jun 18 '18 at 06:59
  • 1
    @METALHEAD - You're not importing `uniq.js`, you're importing `lodash`, which aggregates the contents of the other files into a single module, which it exports as `_` (but also directly), see `lodash.js` which does this: `(freeModule.exports = _)._ = _;` (where `freeModule` is `module` in their bootstrapping, which is complex because it supports multiple module systems). – T.J. Crowder Jun 18 '18 at 07:21
  • Thank you so much for the explanation. – METALHEAD Jun 18 '18 at 07:24