From 7647954a85aafaaaea714aaca0a803b06fa7e11f Mon Sep 17 00:00:00 2001 From: Stevepurpose Date: Mon, 22 May 2023 01:07:51 +0100 Subject: [PATCH] doc: clarify mkdir() recursive behavior --- doc/api/fs.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index cc8bce4b0431fa..48aa130aa474c6 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -3198,23 +3198,29 @@ Asynchronously creates a directory. The callback is given a possible exception and, if `recursive` is `true`, the first directory path created, `(err[, path])`. `path` can still be `undefined` when `recursive` is `true`, if no directory was -created. +created,for instance if the directory was previously created or other issues. The optional `options` argument can be an integer specifying `mode` (permission and sticky bits), or an object with a `mode` property and a `recursive` property indicating whether parent directories should be created. Calling `fs.mkdir()` when `path` is a directory that exists results in an error only -when `recursive` is false. +when `recursive` is false.That is, if we don't add `recursive` as true and +the file was previously created,we get an EEXIST (error Exist) message telling + us path already exists. ```mjs import { mkdir } from 'node:fs'; -// Creates /tmp/a/apple, regardless of whether `/tmp` and /tmp/a exist. -mkdir('/tmp/a/apple', { recursive: true }, (err) => { +//Note the relative path to `tmp` below ,otherwise path won't be created in our project. + +mkdir('./tmp/a/apple', { recursive: true }, (err) => { if (err) throw err; }); ``` +without `recursive` being set as true it the code above,output shows an error + + On Windows, using `fs.mkdir()` on the root directory even with recursion will result in an error: