Skip to content

Commit 3d7ce17

Browse files
jasnellFishrock123
authored andcommitted
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 8fca962 commit 3d7ce17

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

doc/api/fs.md

+31
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,37 @@ fs.mkdtemp('/tmp/foo-', (err, folder) => {
892892
});
893893
```
894894

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

0 commit comments

Comments
 (0)