Node.js v10.12.0からは再帰的にディレクトリを作成できる。



Added a recursive option to fs.mkdir and fs.mkdirSync. If this option is set to true, non-existing parent folders will be automatically created. #21875

resuceなどを使って作成していたのがoption指定で再帰的に作成をしてくれる。Linuxでいうと mkdir -p 

fs.mkdir(path[, options], callback)[src]#

  * path <string> | <Buffer> | <URL>
  * options <Object> | <integer>
     * recursive <boolean> Default: false
     * mode <integer> Not supported on Windows. Default: 0o777.mode 
  * callback <Function>
     * err <Error>

recursive optionをtrueにすると有効になる。

    fs.mkdirSync("/tmp/foo/bar", { recursive: true });


Software Engineer. https://teitei-tk.com