Course Node.js Backend Development

The Module System

Node.js Basics Preview

Modules

Node groups code into modules. You require (CommonJS) or import (ESM) what you need.

Core modules

const fs = require("node:fs");\nconst path = require("node:path");\nconst http = require("node:http");\nconst os = require("node:os");

Your own modules

// math.js\nfunction add(a, b) { return a + b; }\nmodule.exports = { add };\n\n// app.js\nconst { add } = require("./math");\nconsole.log(add(2, 3));   // 5

ES modules

// math.mjs  (or "type": "module" in package.json)\nexport function add(a, b) { return a + b; }\n\n// app.mjs\nimport { add } from "./math.mjs";

package.json

{\n  "name": "my-api",\n  "version": "1.0.0",\n  "main": "index.js",\n  "scripts": {\n    "start": "node index.js",\n    "dev": "node --watch index.js"\n  }\n}

npm basics

npm init -y\n npm install express\n npm install -D nodemon\n npm uninstall something
Tip: Every Node project starts with npm init -y. The node_modules folder it creates should always be gitignored.

This is a free preview lesson

Enroll in the Node.js Backend Development to unlock every lesson, lesson PDF guides, quizzes and your certificate - and start creating your own pieces today.


Discussion

Free preview lesson