← Back to JavaScript
54

Reading and Writing Files Safely in Node

Use `fs/promises` to work with files without blocking the event loop, validate paths defensively, and handle common file-system failures as expected cases instead of surprises.

📘 Theory

Use the Modern Promise-Based File API

Readable async flows and explicit error handling matter more than one-line convenience.

1

The `fs/promises` API fits naturally with `async/await` and keeps file work aligned with the asynchronous model of Node.

2

That is especially important in backend contexts, where synchronous file reads can block other useful work.

Validate Paths Before Operating on Them

Never assume a path built from external input is harmless.

When a path comes from a request, CLI argument, or upload workflow, you should normalize and resolve it before trusting it.

The practical goal is to avoid directory traversal and to keep file operations inside an allowed working area.

  • Use `path.resolve(...)` to normalize the absolute target
  • Restrict operations to a known base directory
  • Reject suspicious traversal patterns before reading or writing

Treat File System Errors as Normal Cases

Missing files and permission failures should be handled, not treated like rare accidents.

A file may not exist. A directory may be passed where a file is expected. Permissions may block access in production even if your local machine works.

When you recognize those failures clearly, your scripts become more stable and your error messages become more useful.

  • `ENOENT`: the file or path does not exist
  • `EACCES`: the process lacks permission
  • `EISDIR`: you tried to read a directory as if it were a file

Where These Patterns Show Up in Real Work

File access appears in more places than beginners expect.

1

Export jobs, report generation, local caches, configuration loading, import tools, and background jobs all rely on file I/O patterns like these.

2

If you can read and write files safely, you gain a practical bridge between JavaScript logic and the operating system around it.

🧪 Learn by doing

Example Guided Example: Read JSON Configuration Load a small JSON file asynchronously and report a clear error if it cannot be read.

🏁 Challenges

Challenge Challenge: Write a Report File Safely Create a function that writes text into a file and handles write failures cleanly.

What is this?

I'm Cristian Eslava and I sometimes build websites so both you and I can learn and experiment. culTest

I made this in February 2026 to make learning easier for my students. The idea is to learn web development by practicing and to keep expanding the project with new topics, tests and challenges.

It draws inspiration from MDN, W3Schools, CodePen, Manz and many other web development references. I wanted to combine useful theory, runnable examples, challenges and the testing system I had already built for culTest. culTest

If you liked it, if you didn't, or if you want to get in touch, write to me at cristianeslava@gmail.com