From 2ccba1f9fa8e593d09f2f4f284e246e1f8bb6549 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Mon, 16 May 2016 21:40:17 -0700 Subject: [PATCH] doc: clarify fs.mkdtemp prefix argument Per: https://github.com/nodejs/node/issues/6142 Clarify the prefix argument. Fixes: https://github.com/nodejs/node/issues/6142 PR-URL: https://github.com/nodejs/node/pull/6800 Reviewed-By: Ben Noordhuis Reviewed-By: Roman Klauke --- doc/api/fs.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/doc/api/fs.md b/doc/api/fs.md index 8fa1fb348dca76..a849cd3d5b7bfa 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -900,6 +900,37 @@ fs.mkdtemp('/tmp/foo-', (err, folder) => { }); ``` +*Note*: The `fs.mkdtemp()` method will append the six randomly selected +characters directly to the `prefix` string. For instance, given a directory +`/tmp`, if the intention is to create a temporary directory *within* `/tmp`, +the `prefix` *must* end with a trailing platform-specific path separator +(`require('path').sep`). + +```js +// The parent directory for the new temporary directory +const tmpDir = '/tmp'; + +// This method is *INCORRECT*: +fs.mkdtemp(tmpDir, (err, folder) => { + if (err) throw err; + console.log(folder); + // Will print something similar to `/tmp-abc123`. + // Note that a new temporary directory is created + // at the file system root rather than *within* + // the /tmp directory. +}); + +// This method is *CORRECT*: +const path = require('path'); +fs.mkdtemp(tmpDir + path.sep, (err, folder) => { + if (err) throw err; + console.log(folder); + // Will print something similar to `/tmp/abc123`. + // A new temporary directory is created within + // the /tmp directory. +}); +``` + ## fs.mkdtempSync(template)