Skip to content

Commit 7647954

Browse files
StevepurposeTrott
authored andcommitted
doc: clarify mkdir() recursive behavior
1 parent c7fe303 commit 7647954

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

doc/api/fs.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -3198,23 +3198,29 @@ Asynchronously creates a directory.
31983198
The callback is given a possible exception and, if `recursive` is `true`, the
31993199
first directory path created, `(err[, path])`.
32003200
`path` can still be `undefined` when `recursive` is `true`, if no directory was
3201-
created.
3201+
created,for instance if the directory was previously created or other issues.
32023202
32033203
The optional `options` argument can be an integer specifying `mode` (permission
32043204
and sticky bits), or an object with a `mode` property and a `recursive`
32053205
property indicating whether parent directories should be created. Calling
32063206
`fs.mkdir()` when `path` is a directory that exists results in an error only
3207-
when `recursive` is false.
3207+
when `recursive` is false.That is, if we don't add `recursive` as true and
3208+
the file was previously created,we get an EEXIST (error Exist) message telling
3209+
us path already exists.
32083210
32093211
```mjs
32103212
import { mkdir } from 'node:fs';
32113213
3212-
// Creates /tmp/a/apple, regardless of whether `/tmp` and /tmp/a exist.
3213-
mkdir('/tmp/a/apple', { recursive: true }, (err) => {
3214+
//Note the relative path to `tmp` below ,otherwise path won't be created in our project.
3215+
3216+
mkdir('./tmp/a/apple', { recursive: true }, (err) => {
32143217
if (err) throw err;
32153218
});
32163219
```
32173220
3221+
without `recursive` being set as true it the code above,output shows an error
3222+
3223+
32183224
On Windows, using `fs.mkdir()` on the root directory even with recursion will
32193225
result in an error:
32203226

0 commit comments

Comments
 (0)