AI Hints
Home›JavaScript Tutorial›Node and npm
JavaScript · Lesson 106 of 138

Node and Files

JavaScript on your computer.

⏱ 2 min read▶ Video at 2:06:53🎓 Free
Watch this lesson in the JavaScript courseWatch on YouTube ↗Full JavaScript course

Node lets JavaScript run outside the browser, on your computer or on a server. There's no page and no DOM. Instead, Node gives you modules for files, networks, and the operating system. It's how JavaScript is used for back ends, tools, and scripts.

Node and Files
From the video: Node and Files
  • JavaScript outside the browser
  • No page, no DOM
  • Files, networks, the system

Reading and writing files

We import the promise version of the f s module, short for file system. Write file creates a file with some text. Read file reads it back, as a string when we give it an encoding. Append file adds to the end. And readdir lists the files in a folder. Run it.

app.mjsjavascript
import { writeFile, readFile, appendFile, readdir } from "node:fs/promises";

await writeFile("notes.txt", "Buy milk\n");
await appendFile("notes.txt", "Call Sam\n");

const text = await readFile("notes.txt", "utf8");
console.log(text.trim().split("\n"));

const files = await readdir(".");
console.log(files.includes("notes.txt"));
Output
[ 'Buy milk', 'Call Sam' ]
true

process and path

Process tells you about the running program. Process dot argv holds command line arguments, and process dot env holds environment variables. The path module joins folder and file names safely, on any operating system. Run it.

app.mjsjavascript
import path from "node:path";

console.log("Node version:", process.version.split(".")[0]);
console.log("Arguments:", process.argv.length);
console.log("Home set:", process.env.HOME !== undefined);

const file = path.join("reports", "2026", "summary.txt");
console.log(file, path.extname(file), path.basename(file));
Output
Node version: v22
Arguments: 2
Home set: true
reports/2026/summary.txt .txt summary.txt

Summary

Node runs JavaScript outside the browser. The f s module reads and writes files, and process and path tell you about the system. Next, npm and package dot JSON.

Key takeaways
  • node app.js
  • fs/promises
  • process, path
Next up: npm — packages and scripts