fs.readFile(file, callback(err, data))

lets say we have a file named data.txt full of lorem text, or for this example, some random text from a news article.


var fs = require('fs');

fs.readFile('data.txt', function(err, data) {
  if (err) throw err;
  if (data) {
    data = data.toString();
    process.stdout.write(`${data} \n`);
  1. require the filesystem module (fs), which is included in the core of node, which means you dont need to install any node packages to use it.
  2. we call fs.readFile() (the async version) passing in the name of the file within the current directory as the first arg
  3. as the second arg we pass in the anonymous cb function, which have the params err for if theres an error, and data for a successful read.
  4. we write an output to the console to confirm the write

fs.writeFile(file, content, cb(err))

lets say we want to create a file, and give a string of content for fs to write to a file.

using the fs async fs.writeFile() we do the following:


var fs = require('fs');

fs.writeFile('data2.txt', 'hello there world and pplz!', function(err) {
  if (err) throw err;
  process.stdout.write('wrote to the file \n');
  1. require the fs module
  2. call the async version of fs.writeFile(), which takes a callback function as the last argument to call once the write is complete

fs.readdir(dir, cb(err, files))

you want to read through a given directory and display each files’ name.


var fs = require('fs')

fs.readdir('.', function(err, files) {
  if (err) throw err;

  for (var file in files) {
    process.stdout.write(`file: ${files[file]} \n`);
  1. require fs module, of course
  2. call fs.readdir() passing the ‘.’ for the current dir and a callback for when the method has completed.
  3. handle possible error.
  4. use a for-in loop to loop over each of the files in the returned array
  5. use process.stdout.write() to write the name of each file passed in from the for-in loop

traversing the filesystem

lets say you want to recursively read through a list of all of the files in a given directory location and display each file’s name or recursively traverse the file if it is a directory.

var fs     = require('fs')
  , stdin  = process.stdin
  , stdout = process.stdout

var traverseFilesystem = function(currentPath) {
  var files = fs.readdirSync(currentPath);
  for (var file in files) {
    var currentFile = `${currentPath}/${files[file]}`;
    var stats = fs.statSync(currentFile);

    if (stats.isFile()) {
      stdout.write(`${currentFile} \n`);
    else if (stats.isDirectory()) {

  1. this program takes in a directory, uses readdirSync, syncronous version of fs.readdir
  2. uses for-in loop to loop over all of the files in returned array
  3. uses syncronous version of fs.stat to get stats on the current file
  4. if the targeted file is a file, write its output
  5. if the targeted file is a directory, use recursion to traverse through the targeted directories individual files.

other fs methods

  • fs.stat(filename, cb(err, stats))
  • fs.rename(filename, new filename, cb(err))
  • fs.chmod(filename, permisor, cb(err))