require()

To use existing modules, we use a function called as 'require()'. 'require()' allows us to do 3 things:

  • Allows us to load the modules that come bundled with Node.js (fs, http, url)
  • Allows us to load third-party modules (express, bluebird)
  • Allows us to load our own files. We can break our application into multiple files, instead of building a single large file, which are difficult to build, debug and maintain.

module.exports

Inside of all node files, we have access to a object - module. To check its properties, we can do - console.log(module).

"exports" is special property in "module" object. In abc.js file, we can create functions, variables etc. and set it on "exports" property, and those properties in abc.js can be used by other file pqr.js, after pqr.js imports abc.js using require.

The keyword

require

returns an object, which references the value of

module.exports

results matching ""

    No results matching ""