V
V
verycooldev2018-07-03 12:37:27
Node.js
verycooldev, 2018-07-03 12:37:27

How to create directories recursively?

What's the easy way to create nested directories?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2018-07-03
@verycooldev

https://www.npmjs.com/package/mkdirp - specific to your task
https://www.npmjs.com/package/fs-extra - extended fs
https://www.npmjs.com/package/shelljs - shell commands in unix style cross-platform
about a clean node, the modules are written on it, no one bothers to do it yourself,
we do it in a simple way, by calling the system command:

const cp = require('child_process');
const path = require('path');
const {promisify} = require('util');
const exec = promisify(cp.exec);

function mkdirp(dirPath) {
  return (process.platform === 'win32'
    ? exec(`mkdir ${path.win32.resolve(dirPath)}`) // win версия рекурсивна по умолчанию
    : exec(`mkdir -p ${path.posix.resolve(dirPath)}`)
  );
}

A little more complicated, but more productive, on the capabilities of the fs module - you can look at the sources of the same shelljs: https://github.com/shelljs/shelljs/blob/master/src... or fs-extra: https://github. com/jprichardson/node-fs-extra/blob...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question