# Node's Built-In Modules, and the require You'll Still See

Node comes with built-in modules for files, file names, the computer and everyday helpers, imported with node in front of their names, and older code shares modules with require and module.exports, which you can read and rewrite as import and export.

Apprentice · Node.js · JavaScript · Updated November 19, 2026

By Gregor Dzierzon

## After this lesson you can

- Import Node's built-in modules, such as node:fs/promises for files or node:path for file names, and choose the one for a job
- Read CommonJS code that uses require and module.exports, tell it from an ES module, and rewrite it with import and export

## Key takeaways

- Node comes with built-in modules, such as node:fs/promises for files, node:path for file names, node:os for the computer and node:util for helpers. There's nothing to install.
- The word node and a colon in front of a module's name, as in node:path, says the module comes with Node, not from npm or from a file of yours.
- CommonJS is Node's older module system. require brings a module in, and module.exports is what a file shares.
- A require line stops with an error in a project whose package.json sets type to module. Rewrite it as an import, and give your own files' paths their .js ending.

Almost every Node program you'll write saves something to a file, works with a file's name, or needs a small helper, and Node already has the code for all of it. You'll also open older projects, and search results, full of lines like `const fs = require("fs")` that look nothing like this course's `import`. After this lesson you can use Node's own toolbox, and read the older style when you meet it.

## A toolbox that comes with Node

Chapter Two Book Club's planner should remember the club's picks until next month, so it needs to save them to a file. JavaScript in a web page can't do that: a page has no way to touch the files on your computer. Node can, because it comes with a set of ready-made modules, like a new kitchen that comes with its own pots and knives. They're called **built-in modules**. (In your textbook, this is Node's *standard library*.)

You import a built-in module the way you import a file of your own, with `node:` in front of its name, read "node colon". Here the planner saves two picks with `writeFile` and reads them back with `readFile`, both from `node:fs/promises`:

```javascript
import { readFile, writeFile } from "node:fs/promises";

const picks = ["The Lantern Library", "A Map of Quiet Rivers"];

await writeFile("picks.json", JSON.stringify(picks));
const text = await readFile("picks.json", "utf8");
console.log(JSON.parse(text));
```

```text
[ 'The Lantern Library', 'A Map of Quiet Rivers' ]
```

`picks.json` is now a file in the folder, and it's still there tomorrow. *Reading and Writing Files* goes through `node:fs/promises` in full.

The text after `from` says where a module comes from:

| The import path | Where the module comes from |
|---|---|
| `"./books.js"` | A file of yours, in this folder |
| `"date-fns"` | A package from npm, in `node_modules` |
| `"node:fs/promises"` | Node itself: built in, nothing to install |

## A short tour: path, os and util

Three more built-in modules come up again and again. **`node:path`** works with file names. A default import brings in the whole module, and you call its functions with a dot:

```javascript
import path from "node:path";

const file = "club/2026/december-picks.json";

console.log(path.basename(file));
console.log(path.extname(file));
console.log(path.basename(file, ".json"));
```

```text
december-picks.json
.json
december-picks
```

`basename` is the file's own name, without its folders, and `extname` is its ending. *Paths and Folders* adds `path.join`, which builds a path that works on Windows and on a Mac.

**`node:os`** tells you about the computer the program runs on:

```javascript
import os from "node:os";

console.log(os.platform());
console.log(os.homedir());
```

On Sam's Windows laptop it prints `win32` and `C:\Users\Sam`. On a Mac it prints `darwin` and a folder such as `/Users/sam`.

**`node:util`** holds helpers. `console.log` shows two levels of nesting inside an object, and prints `[Array]` or `[Object]` for anything deeper. `util.inspect` with `depth: null` shows all of it:

```javascript
import util from "node:util";

const meeting = {
  date: "2026-12-03",
  pick: { title: "A Map of Quiet Rivers", votes: { yes: ["Maya", "Omar", "Lena"], no: ["Sam"] } },
};

console.log(meeting);
console.log(util.inspect(meeting, { depth: null }));
```

```text
{
  date: '2026-12-03',
  pick: {
    title: 'A Map of Quiet Rivers',
    votes: { yes: [Array], no: [Array] }
  }
}
{
  date: '2026-12-03',
  pick: {
    title: 'A Map of Quiet Rivers',
    votes: { yes: [ 'Maya', 'Omar', 'Lena' ], no: [ 'Sam' ] }
  }
}
```

## require: the older way

Say you search for how to read a file in Node, and the answer starts like this:

```javascript
const path = require("node:path");

console.log(path.extname("december-picks.json"));
```

```text
.json
```

That's **CommonJS**, Node's original module system. Node is older than JavaScript's `import` and `export`, so it had its own way: **`require`** brings a module in, and **`module.exports`** is what a file shares. Plenty of projects and packages still use it.

Paste that line into a project whose `package.json` says `"type": "module"`, as this course's do, and run it:

```bash
node old-way.js
```

Node stops on the first line. These are its important lines:

```text
ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '\\?\C:\Users\Sam\nodejs-course\book-club\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
```

The message names the fix: use `import`.

## Telling them apart, and rewriting require

Here is the club's `pagesPerWeek` shared both ways:

| | ES modules (this course) | CommonJS (older code) |
|---|---|---|
| Share a function | `export function pagesPerWeek(...)` | `module.exports = { pagesPerWeek };` |
| Bring it in | `import { pagesPerWeek } from "./books.js";` | `const { pagesPerWeek } = require("./books");` |
| A whole module | `import path from "node:path";` | `const path = require("node:path");` |
| `package.json` | `"type": "module"` | no `"type"`, or `"type": "commonjs"` |
| A file ending that decides on its own | `.mjs` | `.cjs` |

To rewrite a `require` line, keep the names and change the shape: `const { a } = require("x")` becomes `import { a } from "x"`. For a file of your own, add the `.js` ending too: CommonJS finds `./books` without it, and an `import` doesn't. Leave it off, and Node says `Cannot find module` and asks `Did you mean to import "./books.js"?`

This course writes ES modules because they're JavaScript's own standard: the same `import` works in a web page and in Node, and new packages are written that way. Your code can still import an older CommonJS package from npm: Node handles that for you.

## Check yourself

2 questions to try on the lesson page.

## So what

Files, file names and helpers come with Node, one import away, and when a project or an answer uses `require`, you can read it and rewrite it in this course's style in a minute.

## Key terms

- [Built-In Module](https://www.ask-codey.com/glossary/built-in-module)
- [fs Module](https://www.ask-codey.com/glossary/fs-module)
- [path Module](https://www.ask-codey.com/glossary/path-module)
- [CommonJS](https://www.ask-codey.com/glossary/commonjs)
- [ES Modules in Node](https://www.ask-codey.com/glossary/es-modules-in-node)

## Related

- [Modules: import and export](https://www.ask-codey.com/lessons/javascript-modules-import-and-export)
- [npm and Packages](https://www.ask-codey.com/lessons/javascript-tooling-npm-and-packages)

Source: https://www.ask-codey.com/lessons/node-modules-built-in-and-commonjs
