LoginSignup
6
4

Node.jsでディレクトリを再帰的に作成/削除するのにmkdirpやrimrafはもう必要ない

Last updated at Posted at 2021-10-15

Node.jsでディレクトリを再帰的に作成するためのnpmパッケージとしてmkdirp、ディレクトリを再帰的に削除するためのnpmパッケージとしてrimrafがありますが、現代のNode.js(v14.14.0以降)において、それらはもはや必要ありません。

ディレクトリを再帰的に作成する方法

mkdir -pのようにディレクトリを再帰的に作成するには、fs.mkdirrecursiveオプションを使います。

const fs = require('fs')

await fs.promises.mkdir('path/to/dir', { recursive: true })

なお、fs.mkdirrecursiveオプションが追加されたのはv10.12.0です。

ディレクトリを再帰的に削除する方法

rm -rfのようにディレクトリを再帰的に削除するには、fs.rmrecursiveオプションとforceオプションを使います。

const fs = require('fs')

await fs.promises.rm('path/to/dir', { recursive: true, force: true })

なお、fs.rmが追加されたのはv14.14.0です。

6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4