System and Files in Node.js

System and Files is the third chapter of the free Node.js tutorial. It covers talking to the computer: command-line input, settings, signals, files, folders, bytes and streams.

What each lesson covers:

  • Process, OS and CLI Tools: The process object (argv, env, platform, arch, version, pid, cwd, uptime and process events), environment files with --env-file and process.loadEnvFile, standard input, output and error, TTY detection and shell piping, exit codes, process.exit versus process.exitCode, the exit and beforeExit events, signals such as SIGINT and SIGTERM, the os module, and building command-line tools with a shebang, the bin field, util.parseArgs, help output, util.styleText and npm link.
  • Path and File System: Building file paths with the path module (join, resolve, normalize, relative, basename, dirname, extname, parse, format, isAbsolute, sep and delimiter), Windows and POSIX paths, fileURLToPath and import.meta.dirname, the sync, callback and promise fs APIs, reading text, JSON and folders, writing and appending, file flags, safe writes, copying, renaming and deleting, mkdir and mkdtemp, checking existence without race conditions, stat and permissions, symlinks, low-level file handles and watching files.
  • Buffers, Streams and Compression: Buffers and raw bytes (alloc, allocUnsafe, from, encodings, concat, subarray, compare, equals, indexOf, typed arrays, string_decoder), why streams save memory, readable streams (flowing and paused modes, events, async iteration, highWaterMark, Readable.from, createReadStream), writable streams and backpressure, duplex and transform streams, object mode, composing streams with pipe and pipeline, the readline module, gzip, deflate and Brotli compression with zlib, and web streams.

Every lab runs a small Node.js runtime in your browser, in JavaScript or TypeScript (pick one in the Language menu at the top). Examples that need the real operating system show output from real Node.js.

Lessons in this section

  1. Process, OS and CLI Tools

    The process object (argv, env, platform, arch, version, pid, cwd, uptime and process events), environment files with --env-file and process.loadEnvFile, standard input, output and error, TTY detection and shell piping, exit codes, process.exit versus process.exitCode, the exit and beforeExit events, signals such as SIGINT and SIGTERM, the os module, and building command-line tools with a shebang, the bin field, util.parseArgs, help output, util.styleText and npm link.

    • Read arguments, environment variables and program facts from process
    • Load settings from a .env file
    • Write to stdout and stderr and read piped input from stdin
    • Use exit codes and the exit events correctly
  2. Path and File System

    Building file paths with the path module (join, resolve, normalize, relative, basename, dirname, extname, parse, format, isAbsolute, sep and delimiter), Windows and POSIX paths, fileURLToPath and import.meta.dirname, the sync, callback and promise fs APIs, reading text, JSON and folders, writing and appending, file flags, safe writes, copying, renaming and deleting, mkdir and mkdtemp, checking existence without race conditions, stat and permissions, symlinks, low-level file handles and watching files.

    • Build correct paths on every operating system with path
    • Find files next to the current module with import.meta.dirname
    • Read and write text, JSON and folders with fs/promises
    • Write files safely and avoid check-then-act race conditions
  3. Buffers, Streams and Compression

    Buffers and raw bytes (alloc, allocUnsafe, from, encodings, concat, subarray, compare, equals, indexOf, typed arrays, string_decoder), why streams save memory, readable streams (flowing and paused modes, events, async iteration, highWaterMark, Readable.from, createReadStream), writable streams and backpressure, duplex and transform streams, object mode, composing streams with pipe and pipeline, the readline module, gzip, deflate and Brotli compression with zlib, and web streams.

    • Create Buffers and convert between text encodings
    • Explain why streams use less memory than readFile
    • Read with readable streams and async iteration
    • Write with writable streams and respect backpressure