Skip to content
Home Tutorials Roadmaps Courses Classroom Training Blog
Log in Join free
Node.js Intermediate Free

Modules & NPM

Lesson 2 of 17 · Modules & NPM 12%
1. Getting Started
What is Node.js?
2. Modules & NPM
Modules & NPM
6. Authentication & Security
JWT Authentication Password Hashing
8. WebSockets & Queues
Socket.io Message Queues
9. Process & Environment
Process & Environment Variables
10. Caching & Performance
Redis Caching
11. GraphQL with Node.js
GraphQL Basics

Node.js uses modules to organize code. You can use built-in modules or install packages from NPM.

Module Types

  • Core modules: fs, http, path, os
  • Local modules: your own files
  • Third-party: installed via NPM

Syntax

NODEJS
// Import a module
const fs = require('fs');

// ES Module
import express from 'express';

// Export
module.exports = { myFunction };
Node.js Modules
NODE
// math.js
function add(a, b) { return a + b; }
function subtract(a, b) { return a - b; }
module.exports = { add, subtract };

// app.js
const math = require("./math");
console.log(math.add(5, 3));
console.log(math.subtract(10, 4));

// ES modules
// import { add } from "./math.js";

Practice

1
Exercise

How do you import a built-in module in Node.js?

Answer
const fs = require('fs');

Quick Quiz

1

What does NPM stand for?

NPM stands for Node Package Manager.

Choose an answer to continue.

Interview Questions

require is CommonJS (Node.js default). import is ES Modules (modern standard). Both load modules.

Up next

Reading & Writing Files

Lesson 3 of 17 · File System & Streams