Node.js: create directory recursive


fs.mkdirSync

Synchronously creates a directory:

const fs = require('fs');
const path = 'a';

fs.mkdirSync(path, { recursive: true });

fs.mkdir

Asynchronously creates a directory:

const fs = require('fs');
const path = 'a/b';

fs.mkdir(path, { recursive: true }, error => {
  if (error) {
    throw error;
  }
});

fs.promises.mkdir

Asynchronously creates a directory with a promise:

(async () => {
  try {
    const path = 'a/b/c';
    await fs.promises.mkdir(path, { recursive: true });
  } catch (error) {
    throw error;
  }
})();

Demo

Repl.it:



Please support this site and join our Discord!