mkdir -p and file copying in Node.js

mkdir -p and file copying in Node.js

mkdir -p, wherein you create a directory, including all its parent directories if they don't exist, is functionality that is not provided out of the box by Node, but is required fairly commonly.

There's a Node package node-fs-extra that comes to our rescue!

To use it:

npm install --save node-fs-extra

const fs = require("fs-extra");
fs.mkdirp("/tmp/fileData/html").then(function (res) {
    // do something with response
}).catch(function (err) {
    // do something with error
});

And to copy a file, use

fs.copy("srcFile", "destFile", {}).then(function (res) {
    // do something with response
}).catch(function (err) {
    // do something with error
});