DevPath · Learn to code ESPTEN

Node.js: runtime, modules and async

Modules and npm

Splitting code into modules

A real application does not fit in a single file. Node lets you separate code into modules (files) that export and import functionality. There are two systems that will coexist for a long time.

CommonJS (Node's classic)

Uses require to import and module.exports to export:

// math.js
function add(a, b) { return a + b; }
module.exports = { add };

// app.js
const { add } = require("./math");
console.log(add(2, 3)); // 5

ES Modules (the modern standard)

It is the language's standard syntax, the same as in the browser. It uses import / export:

// math.js
export function add(a, b) { return a + b; }

// app.js
import { add } from "./math.js";

To use ESM in Node you set "type": "module" in the package.json (or you use the .mjs extension). Key differences: require is synchronous and dynamic (you can call it inside an if); import is static (resolved before running) and allows optimizations.

package.json and npm

The package.json is the project's manifest: name, version, scripts and dependencies. With npm (Node's package manager) you install libraries:

{
  "name": "my-app",
  "type": "module",
  "scripts": { "start": "node app.js" },
  "dependencies": { "express": "^4.18.0" }
}

npm install express downloads the package to node_modules and records it in dependencies. Then you import it by its name: import express from "express".

The standard library

Node ships with built-in modules (no need to install them):

Examples

Import a module from the standard library (CommonJS)

const path = require("path");
console.log(path.join("folder", "file.txt")); // "folder/file.txt"
console.log(path.extname("photo.png"));        // ".png"
Put this into practice

DevPath is a hands-on course: you read the theory here; in the app you put it into practice with exercises that really run, offline.

Start free in the app →
← What is Node.jsAsynchrony in Node →