Working with the filesystem module in node.js

Working with files is a big part of most node.js projects. I have written a post on fs-extra a while back, but so far never got around to the core file system module in node.js itself.

Basic read file example

To start off this post how about a simple read file example. Use of the method is fairly simple, I just call fs.readFile, and pass the path of the file I want to read, following with the character encoding, and finally a callback that will be called when the file is read.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
let fs = require('fs'),
path = require('path'),
cwd = process.cwd();
fs.readFile(path.join(cwd, 'README.md'), 'utf-8', function (e, data) {
if (e) {
console.log(e);
}
console.log(data);
});

I am using the path module, because I have found that is a much better way of handling paths compared to just concatenating strings. In addition the cwd method of the process object will always return the current working directory which can be useful, but that is another post, for another day.

Read a file Promise style

As of node.js 8.x all the methods do not return promises, this is one of the reasons why you might want to use fs-extra, but it’s not like it is that hard to make a method that will return a promise.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
let fs = require('fs'),
path = require('path'),
cwd = process.cwd();
// read file method
let readFile = function (dir) {
// returns a promise
return new Promise(function (resolve, reject) {
// read a file
fs.readFile(dir, 'utf-8', function (e, data) {
if (e) {
// reject if error
reject(e);
}
// else resolve with the data
resolve(data);
});
});
};
// read a file Promise style
readFile(path.join(cwd, 'README.md')).then(function (data) {
console.log(data);
}).catch (function (e) {
console.log(e);
});