fs mkdir file system method in nodejs

So when it comes to making a new folder in nodejs there is the fs mkdir method that can be used to make a folder. There is also how to go about making a whole bunch of folders recursively, depending on the version of node that you are using doing so can be very simple, or a little hard.

1 - fs mkdir and making folders recursively

Say you want to use mkdir to make a full path if it is there or not, rather than just one folder at a time. Also say that you want to make it so the method will work on older versions of nodejs, before doing so was very simple. There is mush to write about when it comes to what is available in user space which it comes to npm packages such as mkdirp, and fs-extra when it comes to adding this functionality to a project.

Looking over the source code of mkdirp the basic idea might look something like this:

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
var fs = require('fs'),
path = require('path');
// mkdirp
var mkdirp = function (p, cb) {
p = path.resolve(p);
fs.mkdir(p, function (e) {
if (!e) {
cb(null);
} else {
if (e.code === 'ENOENT') {
// if 'ENOENT' code error call mkdirp
// again with the dirname of current dir
mkdirp(path.dirname(p), function (e) {
if (e) {
cb(e);
} else {
mkdirp(p, cb);
}
});
} else {
// else some other error happed
cb(e);
}
}
});
};
mkdirp('./test/foo/bar', function (e) {
if (!e) {
fs.writeFile('./test/foo/bar/baz.txt', 'okay', function () {
console.log('wrote a file')
});
} else {
console.log(e.message);
}
});

If you are using a late version of nodejs (10.x or later), and you do not care about supporting older versions of node, then using something like this is redundant as the recursive functionality is now baked into nodejs itself.