Modules and files

Split code into modules and read files without blocking.

Add "type": "module" to package.json to use import and export.

Node.js
// gst.js
export const gst = (amount, rate = 18) => (amount * rate) / 100;

// app.js
import { readFile } from "node:fs/promises";
import { gst } from "./gst.js";

console.log(gst(1000));
const text = await readFile("notes.txt", "utf8");
console.log(text);

Use the promise versions of file functions. The Sync versions stop the whole server until they finish.

โ† Getting started with Node.jsYour first Express API โ†’