Tutorial

Introduction to the fs Module in Node.js

Published on June 16, 2019
author

Joshua Hall

Introduction to the fs Module in Node.js

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.

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.

Getting Started

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');

Synchronous vs Asynchronous

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.

Basic Operations

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()

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.

gator.js
const gators = [{
  type: 'cayman'
}];

fs.writeFileSync('./swamp/cayman.json', JSON.stringify(gators));

Result:

swamp/cayman.json
[{"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.

fs.readFileSync()

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.

gator.js
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();

unlinkSync()

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');

CRUD Operations On Folders

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.

rimraf vs fs.rmdirSync()

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');

Conclusion

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.

Learn more about us


About the authors
Default avatar
Joshua Hall

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


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!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel