Newby Coder header banner

Module

What is a Module

A module is a part of a program

Like a book can contain chapters and sections, a program can be divided into modules, although modules can be independently developed

It is a piece of code (like a set of functions, a file or directory) which can be distributed and can be independently created and maintained

The Linux kernel is a modular program, containing a number of modules for different operations (like wifi, encryption, buffering videos etc), which can be loaded into the kernel without requiring a system reboot

To list all active kernel modules, run lsmod command:

cl-linux-cmd-lsmod

Python modules

In Python, modules refer to a file containing Python statements and definitions

A file containing Python code, for example: ncthon.py, is called a module, and its module name is ncthon

Modules are used to divide large programs into small manageable and organized files

import statement is used to import a module

To import specific functions or objects from a module, from keyword is used in an import statement

import os
print(os.getcwd())

In Java, Javascript, node.js, modules have to be explicitly exported using export keyword

Java introduced modules in Java 9

Javascript modules

ECMAScript or (ES) is a standard for scripting languages like JavaScript, ActionScript and JScript

ES6 introduced built-in modules for Javascript

ES6 modules are stored in files, such that, there is one module per file and one file per module

Modules have to be exported using export

For example, a file can export functions like

export const sqrt = Math.sqrt;
export function square(x) {
    return x * x;
}
export function diag(x, y) {
    return sqrt(square(x) + square(y));
}

which can be imported as

import { square, diag } from 'lib';
console.log(square(11)); // 121
console.log(diag(4, 3)); // 5