Getting Started With Modules in Node.js

Introduction

This article explains the modules in Node.js. The module system is implemented in the required ("module") module.

Modules in Node.js

Modules are part of  the program. Programs are composed of one or more independently developed modules that are not combined until the programs is linked. The structure of the module is identical to the structure of the program. Node has a simple module loading sytem. In Node there is a one-to-one correspondence of files and modules. As I have shown you in the example below, Module.js loads the module RequireModule.js in the same directory.

Write the following code in Notepad and save the file with the .js extention.

Module

Now define the module RequireModule.js that is loaded into Module.js:

CallFunction

Now open the Node.js commond prompt and execute the following code:

OpenCmd

Now write the following command in the Command Prompt:

AreaofCircle 

In the module.js module we have defined the function area(). To add functions and objects to the root of your module you can use the "exports" object. Variables are in modules as private as though the module was wrapped in a function. In the example above the variable PI is private to requiremoduel.js. If you want to export an object in one assignment then assign it to "module.exports" instead of "exports".

For example:

Create a file and save this file with a .js extension as in the following figure:

Square

Now create a module in a square.js file as in the following figure:

Module

Now in a node.js command prompt execute the module as in the following figure:

FindSquare

In the example given above you see the bar.js file uses the square module and the square module is defined in Square.js.

Core Module

The core modules are defined in node's "lib" folder.

  • Node has several module compiled into the binary.
  • Core modules are loaded if their identifier is passed to require().

Sevral modules are compiled into binary in node, for example require('http') returns the builtin HTTP module, even if that filename is there.

File Modules

In node if we don't get the exact file name then node loads the required filename with the extension .js,. json and .node. The .js files are interpreted as JavaScript textfiles and .json files are parsed as JSON text files and the .node files are interpreted as compiled addon modules loaded with dlopen.

A module prefixed with '/' is an absolute path to the file, for examle:

require('/myfolder1/myfolder2/filename.js')

A module prefixed with './' refers to the file calling require(). That is circle.js must be in the same directory as foo.js for require('./square') to find it. If we don't use the '/' or './' then that means the module is either a "CORE MODULE" or is loaded from a "node_modules" folder.

If the path does not exist in node then require will throw the error "MODULE_NOT_FOUND."

Load from node_modules Folders

If the module identifier passed to require() is not a native module and does not begin with '/', './' then node starts at the parent directory of the current module and adds /node_modules and loads the files from that location.

For example if the file at /"folder1/projects/node_modules/filename.js" calls requrie('filename.js') then node would look in the following location:

  • /folder1/projects/node_modules/filename.js
  • /folder1/node_modules/filename.js
  • /node_modules/filname.js

Summary

This article has explained modules in Node.js, such ase core modules, file modules and how to load the files from the node_module folder.

Up Next
    Ebook Download
    View all
    Learn
    View all