Preparing your workspace
Modules divide application code into reusable files, while npm manages packages, scripts, and project metadata.
Node.js applications are usually divided into multiple modules so each file can have a clear responsibility. Modern Node.js supports ECMAScript modules, while many existing projects also use CommonJS. npm is the standard package manager commonly used with Node.js projects. It creates package.json, installs dependencies, and can run project scripts. Understanding modules and npm is essential because almost every practical Node.js application depends on external packages and reusable internal modules.
A backend project may keep authentication, database access, user services, and API routes in separate modules while npm manages packages such as Express.
export function add(a, b) {
return a + b;
}
// app.js
import { add } from './math.js';
console.log(add(10, 5));The terminal prints 15.