fs, short for File System, is one of the most basic and useful modules in Node. In this article, we’ll go over some of the most important and useful methods for manipulating the file system.
You’ll need to have Node.js installed first, of course. You can do that here. Luckily for us, fs is one of the ‘out-of-the-box’ modules that come with Node so it is already available to you.
const fs = require('fs');
Every fs method has both a synchronous and an asynchronous version with the name for the synchronous form just ending with Sync. So an asynchronous fs.writeFile()
becomes fs.writeFileSync()
. Of course, Synchronous code will stop later code from running when there is an error, but they’re simpler to get the hang of so for all of the examples in this article we’ll be using their synchronous forms.
The basic CRUD operations (create, read, update, and delete) are very simple to do with only 3 main functions.
fs.writeFileSync()
fs.readFileSync()
fs.unlinkSync()
fs.writeFileSync()
only takes two arguments; the path to the new file’s location, which must end with the name of the new file and the data you want stored.
const gators = [{
type: 'cayman'
}];
fs.writeFileSync('./swamp/cayman.json', JSON.stringify(gators));
Result:
[{"type":"cayman"}]
Keep in mind that fs.writeFileSync()
is completely rewriting the content in cayman.json so if you change type: 'cayman'
to type: 'croc'
and re-run the file, cayman will be replaced. This can have significant performance drawbacks when modifying large files.
By default all data is returned as a ‘buffer’, a special encoded string of numbers, to fix this just pass ‘utf8’ as the second argument.
Here we’re going to return a empty array if cayman.json
is empty and return the data if it exists.
const gators = [{
type: 'cayman'
}];
const getData = () => {
let data = fs.readFileSync('./swamp/cayman.json', 'utf8');
if (!data) return [];
else {
const file = JSON.parse(data);
return file;
}
}
const data = getData();
Unlink is the simplest of the three, only requiring the path to the file or symbolic link you wish to remove.
fs.unlinkSync('./swamp/cayman.json');
The above three methods have their own useful counterparts for manipulating directories themselves.
fs.mkdirSync()
fs.rmdirSync()
fs.readdirSync()
Since the usage of fs.mkdirSync()
and fs.readdirSync()
are the same as their file counterparts we’ll finish up with working out the gotchas that come with fs.rmdirSync()
instead.
The main problem with using fs.rmdirSync()
to delete folders is the fact that it will only work on empty directories. If you were to try using it to remove ./swamp
it will return:
Error: ENOTEMPTY: directory not empty, rmdir './swamp'
which obviously isn’t what we want. Sadly, Node does not have a native solution for this, for some reason, so we’ll have to look elsewhere.
The simplest solution I’ve found has been an npm package called rimraf, so let’s install that quickly.
$ npm i rimraf
The syntax is very similar, except instead of fs you call rimraf.sync()
( or plain rimraf()
for the asynchronous version) and pass in your path as you would expect.
const rimraf = require('rimraf');
rimraf.sync('./swamp');
With just these few functions you’ll be able to handle many of the common use cases with manipulating files and their data.
Any introduction to a new technology will come with obligatory referral to the documentation (it really is one of the best ways to explore something).
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.