Reading-Notes


Project maintained by eslamakram Hosted on GitHub Pages — Theme by mattgraham

FUNCTIONAL PROGRAMMING

Node JS - Modules and require()

  1. Core Modules
  2. Local Modules
  3. Third Party Modules

var module = require('module_name');

As per above syntax, specify the module name in the require() function. The require() function will return an object, function, property or any other JavaScript type, depending on what the specified module returns.

const moduleName = require(‘./filename.js’); You don’t have to add the “.js” extension, Node.js can still load your local module without it as we have learned.

const moduleName = require(‘./filename’); Core ModulesPermalink These are modules that come with Node.js by default. You do not have to download them in your project.

Some of the most popular and frequently used core modules are fs, os, path, etc.

Importing core modules

To import a core module, you have to use the require() method with the core module’s name passed as the argument.

const fileSystem = require(“fs”); Third-Party ModulesPermalink Third-party modules are modules that are downloaded with a package manager such as npm. These modules are usually stored in the node_modules folder.

You can install third-party modules globally or locally in your project.

Examples of third party modules are express, mongoose, react, etc.

Importing Third-Party Modules

To import a third-party module, you have to use the require() method that takes the third-party module’s name as an argument.

const fileSystem = require("express");