ELEVATE YOUR BUSINESS WITH

Limitless customization options & Elementor compatibility let anyone create a beautiful website with Valiance.

Modules in NodeJs

SELECT * FROM `itio_tutorial_master` WHERE `tutorial_menu`='22' AND `tutorial_submenu`='1378' AND `tutorial_status`=1 LIMIT 1

Modules in NodeJs

In Node.js, modules are the building blocks of your application โ€” they help you organize your code into reusable chunks.


๐Ÿ“ฆ What is a Module in Node.js?

A module is simply a file in Node.js.

  • Every .js file is its own module.

  • Modules can export functionality (functions, variables, objects).

  • Other files can import that functionality using require() or import.


๐Ÿงฑ 3 Types of Modules

TypeExample
Core Modulesfs, http, path, url
Local ModulesYour own files (./logger.js)
Third-partyInstalled via npm (express, etc.)


โœ… Local Module Example

1. math.js โ€” your own module

js

const math = require('./math');console.log(math.add(5, 3)); // Output: 8


๐ŸŒ Core Module Example

js

const path = require('path');const filePath = path.join(__dirname, 'test.txt');console.log(filePath);


๐Ÿง™โ€โ™‚๏ธ How Exporting Works

You can export a function, object, class, or anything else:

js

// file: greet.jsmodule.exports = function(name) { return `Hello, ${name}`;};

js

// file: app.jsconst greet = require('./greet');console.log(greet('Alice')); // Hello, Alice


๐Ÿงช ES6 Modules (import/export)

If you're using "type": "module" in package.json, you can use ES6 syntax:

js

// math.jsexport function add(a, b) { return a + b;}// app.jsimport { add } from './math.js';console.log(add(2, 3));


๐Ÿ“š Common Core Modules

ModuleUse
fsFile system (read/write files)
httpCreate servers
pathFile paths
urlParse and format URLs
eventsEvent handling
osGet system info

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql