Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    NODENode.js

    Runtime and core tooling

    • What is Node.js?
    • CommonJS and ES modules
    • npm and package.json
    • process and command-line apps

    Async I/O and events

    • Event loop: callbacks to async/await
    • Files and paths with fs/promises
    • EventEmitter
    • Streams and backpressure

    HTTP and APIs

    • Your first HTTP server
    • Routing and JSON bodies
    • Build a tiny router and middleware chain
    • REST: resources and status codes
    • Validation and error handling

    Production: security, testing and deployment

    • Authentication basics with crypto
    • Configuration and environment secrets
    • Testing with assert and node:test
    • API security fundamentals
    • Project: tasks REST API and deployment
    Node.js

    Async I/O and events · Lesson 6 of 18

    Files and paths with fs/promises

    Read and write safely, and build paths that work across operating systems.

    12 minutes2 games

    node:fs/promises provides file-system APIs returning Promises. Specify an encoding such as utf8 or receive a Buffer. Use node:path rather than joining paths with / because Windows and Linux differ.

    path.join builds a path

    writeFile

    OS filesystem

    readFile + utf8

    Node.js
    const fs = require("node:fs/promises")
    const path = require("node:path")
    
    async function main() {
      const file = path.join("/tmp", "seacode-node-file.txt")
      await fs.writeFile(file, "sea\ncode\n", "utf8")
      const text = await fs.readFile(file, "utf8")
      console.log(text.trim().split("\n").join(" + "))
      await fs.unlink(file)
    }
    main().catch(console.error)

    Handle errors such as ENOENT according to meaning: optional configuration may use a default, but required data should fail clearly. For sensitive writes, write a temporary file then rename it to reduce partial-file risk if the process stops.

    Important

    Never place user input directly in a path; ../../secret can cause path traversal. Resolve against a base directory and verify the result remains inside it, or map trusted IDs to paths.

    Quick question

    What does readFile(file) return without an encoding?

    Interview question

    Sync or async fs APIs in a server?

    Exercise

    Write hello to /tmp/seacode-exercise.txt, read it uppercase, then delete it. Print HELLO.

    Expected output: HELLO

    Node.js
    const fs = require("node:fs/promises")
    async function main() {
      const file = "/tmp/seacode-exercise.txt"
    
    }
    main().catch(console.error)

    Learn more from

    • Node.js: File system promisesnodejs.org
    • OWASP: Path Traversalowasp.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousEvent loop: callbacks to async/awaitNextEventEmitter
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.