Understanding Node.js Modules with fs: require, import, and Sync Methods
Learn what modules are in Node.js, how the built-in fs module works, why require exists, the newer import syntax, what Sync means, and why utf-8 is used.
May 28, 2026
Understanding Node.js Modules with fs: require, import, and Sync Methods
When writing Node.js apps, one of the first concepts you’ll use is modules.
Modules are how Node organizes reusable code so you don’t write everything in one file.
In this post, we’ll cover:
What modules are
Why modules are useful
Using the built-in fs module for file read/write
Why we use require
The newer import syntax
What Sync means in methods like readFileSync and writeFileSync
Where to find official docs for built-in modules
1) What are modules in Node.js?
A module is a file (or package) that contains code you can reuse in other files.
Instead of rewriting logic again and again, you place it in a module and load it wherever needed.
Examples:
Built-in modules from Node.js (fs, path, http, os)
Your own local modules (./utils.js, ./logger.js)
Third-party modules from npm (express, , etc.)
mongoose
So when we say “modules have reusable code,” this is exactly what we mean.
2) Why modules are important
Modules help you:
Keep code organized
Reuse logic across files/projects
Make code easier to test and maintain
Avoid one giant file with everything inside
This becomes very important as projects grow.
3) Using the built-in fs module
fs stands for file system.
It lets your Node.js app interact with files and directories—like reading, writing, renaming, deleting, etc.
Example:
const fs = require('fs');
const file = fs.readFileSync('<path>', 'utf-8');
console.log(file);
// node index.js // to run the file
const text = `Let us add new things before ${file}`;
fs.writeFileSync('<path>', text);
In this snippet:
fs.readFileSync(...) reads file content
fs.writeFileSync(...) writes new content to a file
In fs.readFileSync('<path>', 'utf-8'), the 'utf-8' part is the text encoding. It tells Node.js to decode the file bytes into a normal JavaScript string using UTF-8. If you skip this encoding, Node returns a Buffer (raw binary data) instead of readable text. Since UTF-8 is the standard encoding for most text files on the web (.txt, .json, .md, .js), it is usually the correct default to use.
4) Why do we use require?
require() is the traditional CommonJS way to load modules in Node.js.
const fs = require('fs');
Here, Node loads the built-in fs module and gives it to the fs variable so you can call its methods.
You’ll still see require in many projects, tutorials, and older codebases.
5) Newer way: import
The modern module system is ES Modules (ESM) using import.
Equivalent example:
import fs from 'node:fs';
const file = fs.readFileSync('<path>', 'utf-8');
console.log(file);
You can use ESM by either:
Setting "type": "module" in package.json, or
Using .mjs file extension
So both exist today:
require -> CommonJS (older, still common)
import -> ES Modules (newer, standard JavaScript style)
6) What does Sync mean here?
In readFileSync and writeFileSync, Sync = synchronous.
That means Node executes the operation and waits until it finishes before moving to the next line.
Practical meaning
readFileSync() blocks execution until file is fully read
writeFileSync() blocks execution until file is fully written
This is simple and easy for scripts, quick utilities, and learning.
But for larger apps
In production servers, async methods are usually preferred:
fs.readFile(...)
fs.writeFile(...)
or fs/promises
Why? Async operations avoid blocking the event loop, so your server can handle other requests while file operations are in progress.
7) Where to find built-in module details
For complete and up-to-date details of built-in Node.js modules (including fs), use the official Node.js docs:
This is the best source for method behavior, options, versions, and examples.
Final thoughts
Modules are the foundation of writing clean Node.js code.
fs is one of the most useful built-in modules when working with files. Start with require and Sync methods to understand the basics, then move to import and async APIs as your applications scale.