Skip to content

Commit 2ccba1f

Browse files
committed
doc: clarify fs.mkdtemp prefix argument
Per: #6142 Clarify the prefix argument. Fixes: #6142 PR-URL: #6800 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Roman Klauke <[email protected]>
1 parent 56ae651 commit 2ccba1f

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

doc/api/fs.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,37 @@ fs.mkdtemp('/tmp/foo-', (err, folder) => {
900900
});
901901
```
902902

903+
*Note*: The `fs.mkdtemp()` method will append the six randomly selected
904+
characters directly to the `prefix` string. For instance, given a directory
905+
`/tmp`, if the intention is to create a temporary directory *within* `/tmp`,
906+
the `prefix` *must* end with a trailing platform-specific path separator
907+
(`require('path').sep`).
908+
909+
```js
910+
// The parent directory for the new temporary directory
911+
const tmpDir = '/tmp';
912+
913+
// This method is *INCORRECT*:
914+
fs.mkdtemp(tmpDir, (err, folder) => {
915+
if (err) throw err;
916+
console.log(folder);
917+
// Will print something similar to `/tmp-abc123`.
918+
// Note that a new temporary directory is created
919+
// at the file system root rather than *within*
920+
// the /tmp directory.
921+
});
922+
923+
// This method is *CORRECT*:
924+
const path = require('path');
925+
fs.mkdtemp(tmpDir + path.sep, (err, folder) => {
926+
if (err) throw err;
927+
console.log(folder);
928+
// Will print something similar to `/tmp/abc123`.
929+
// A new temporary directory is created within
930+
// the /tmp directory.
931+
});
932+
```
933+
903934
## fs.mkdtempSync(template)
904935
<!-- YAML
905936
added: v5.10.0

0 commit comments

Comments
 (0)