Skip to the content.

Node.js modules

Node.js contains a lot of modules allowing ot perform most of required functions to work with filesystem, http, processes etc.

Most of them provide basic functionality and it is more common nowadays to use special solutions to solve these tasks.

As an example it is rarely used http (https) to make API requests, but rather axios which provide more nice options and packed with additional functionality

You could install all Node.js modules type definitions:

yarn add @types/node -D

Some popular modules:

And much more https://nodejs.org/docs/latest-v13.x/api/

import { unlinkSync } from "fs";

try {
  unlinkSync("/tmp/hello");
  console.log("successfully deleted /tmp/hello");
} catch (err) {
  // handle the error
}

Hint: Cannot find module 'fs' or its corresponding type declarations. error happening when no @types/node is installed

fs

Recommended filesystem access pattern:

import { promises: fs } from "fs";
const contents = await fs.readFile(name);

No need to use callback or manual Promise wrapping