3 A simple temporary file and directory creator for [node.js.][1]
5 [![Build Status](https://travis-ci.org/raszi/node-tmp.svg?branch=master)](https://travis-ci.org/raszi/node-tmp)
6 [![Dependencies](https://david-dm.org/raszi/node-tmp.svg)](https://david-dm.org/raszi/node-tmp)
7 [![npm version](https://badge.fury.io/js/tmp.svg)](https://badge.fury.io/js/tmp)
8 [![API documented](https://img.shields.io/badge/API-documented-brightgreen.svg)](https://raszi.github.io/node-tmp/)
9 [![Known Vulnerabilities](https://snyk.io/test/npm/tmp/badge.svg)](https://snyk.io/test/npm/tmp)
13 This is a [widely used library][2] to create temporary files and directories
14 in a [node.js][1] environment.
16 Tmp offers both an asynchronous and a synchronous API. For all API calls, all
17 the parameters are optional. There also exists a promisified version of the
18 API, see (5) under references below.
20 Tmp uses crypto for determining random file names, or, when using templates,
21 a six letter random identifier. And just in case that you do not have that much
22 entropy left on your system, Tmp will fall back to pseudo random numbers.
24 You can set whether you want to remove the temporary file on process exit or
25 not, and the destination directory can also be set.
35 Please also check [API docs][4].
37 ### Asynchronous file creation
39 Simple temporary file creation, the file will be closed and unlinked on process exit.
42 var tmp = require('tmp');
44 tmp.file(function _tempFileCreated(err, path, fd, cleanupCallback) {
47 console.log('File: ', path);
48 console.log('Filedescriptor: ', fd);
50 // If we don't need the file anymore we could manually call the cleanupCallback
51 // But that is not necessary if we didn't pass the keep option because the library
52 // will clean after itself.
57 ### Synchronous file creation
59 A synchronous version of the above.
62 var tmp = require('tmp');
64 var tmpobj = tmp.fileSync();
65 console.log('File: ', tmpobj.name);
66 console.log('Filedescriptor: ', tmpobj.fd);
68 // If we don't need the file anymore we could manually call the removeCallback
69 // But that is not necessary if we didn't pass the keep option because the library
70 // will clean after itself.
71 tmpobj.removeCallback();
74 Note that this might throw an exception if either the maximum limit of retries
75 for creating a temporary name fails, or, in case that you do not have the permission
76 to write to the directory where the temporary file should be created in.
78 ### Asynchronous directory creation
80 Simple temporary directory creation, it will be removed on process exit.
82 If the directory still contains items on process exit, then it won't be removed.
85 var tmp = require('tmp');
87 tmp.dir(function _tempDirCreated(err, path, cleanupCallback) {
90 console.log('Dir: ', path);
97 If you want to cleanup the directory even when there are entries in it, then
98 you can pass the `unsafeCleanup` option when creating it.
100 ### Synchronous directory creation
102 A synchronous version of the above.
105 var tmp = require('tmp');
107 var tmpobj = tmp.dirSync();
108 console.log('Dir: ', tmpobj.name);
110 tmpobj.removeCallback();
113 Note that this might throw an exception if either the maximum limit of retries
114 for creating a temporary name fails, or, in case that you do not have the permission
115 to write to the directory where the temporary directory should be created in.
117 ### Asynchronous filename generation
119 It is possible with this library to generate a unique filename in the specified
123 var tmp = require('tmp');
125 tmp.tmpName(function _tempNameGenerated(err, path) {
128 console.log('Created temporary filename: ', path);
132 ### Synchronous filename generation
134 A synchronous version of the above.
137 var tmp = require('tmp');
139 var name = tmp.tmpNameSync();
140 console.log('Created temporary filename: ', name);
145 ### Asynchronous file creation
147 Creates a file with mode `0644`, prefix will be `prefix-` and postfix will be `.txt`.
150 var tmp = require('tmp');
152 tmp.file({ mode: 0644, prefix: 'prefix-', postfix: '.txt' }, function _tempFileCreated(err, path, fd) {
155 console.log('File: ', path);
156 console.log('Filedescriptor: ', fd);
160 ### Synchronous file creation
162 A synchronous version of the above.
165 var tmp = require('tmp');
167 var tmpobj = tmp.fileSync({ mode: 0644, prefix: 'prefix-', postfix: '.txt' });
168 console.log('File: ', tmpobj.name);
169 console.log('Filedescriptor: ', tmpobj.fd);
172 ### Controlling the Descriptor
174 As a side effect of creating a unique file `tmp` gets a file descriptor that is
175 returned to the user as the `fd` parameter. The descriptor may be used by the
176 application and is closed when the `removeCallback` is invoked.
178 In some use cases the application does not need the descriptor, needs to close it
179 without removing the file, or needs to remove the file without closing the
180 descriptor. Two options control how the descriptor is managed:
182 * `discardDescriptor` - if `true` causes `tmp` to close the descriptor after the file
183 is created. In this case the `fd` parameter is undefined.
184 * `detachDescriptor` - if `true` causes `tmp` to return the descriptor in the `fd`
185 parameter, but it is the application's responsibility to close it when it is no
189 var tmp = require('tmp');
191 tmp.file({ discardDescriptor: true }, function _tempFileCreated(err, path, fd, cleanupCallback) {
193 // fd will be undefined, allowing application to use fs.createReadStream(path)
194 // without holding an unused descriptor open.
199 var tmp = require('tmp');
201 tmp.file({ detachDescriptor: true }, function _tempFileCreated(err, path, fd, cleanupCallback) {
205 // Application can store data through fd here; the space used will automatically
206 // be reclaimed by the operating system when the descriptor is closed or program
211 ### Asynchronous directory creation
213 Creates a directory with mode `0755`, prefix will be `myTmpDir_`.
216 var tmp = require('tmp');
218 tmp.dir({ mode: 0750, prefix: 'myTmpDir_' }, function _tempDirCreated(err, path) {
221 console.log('Dir: ', path);
225 ### Synchronous directory creation
227 Again, a synchronous version of the above.
230 var tmp = require('tmp');
232 var tmpobj = tmp.dirSync({ mode: 0750, prefix: 'myTmpDir_' });
233 console.log('Dir: ', tmpobj.name);
236 ### mkstemp like, asynchronously
238 Creates a new temporary directory with mode `0700` and filename like `/tmp/tmp-nk2J1u`.
241 var tmp = require('tmp');
243 tmp.dir({ template: '/tmp/tmp-XXXXXX' }, function _tempDirCreated(err, path) {
246 console.log('Dir: ', path);
250 ### mkstemp like, synchronously
252 This will behave similarly to the asynchronous version.
255 var tmp = require('tmp');
257 var tmpobj = tmp.dirSync({ template: '/tmp/tmp-XXXXXX' });
258 console.log('Dir: ', tmpobj.name);
261 ### Asynchronous filename generation
263 The `tmpName()` function accepts the `prefix`, `postfix`, `dir`, etc. parameters also:
266 var tmp = require('tmp');
268 tmp.tmpName({ template: '/tmp/tmp-XXXXXX' }, function _tempNameGenerated(err, path) {
271 console.log('Created temporary filename: ', path);
275 ### Synchronous filename generation
277 The `tmpNameSync()` function works similarly to `tmpName()`.
280 var tmp = require('tmp');
281 var tmpname = tmp.tmpNameSync({ template: '/tmp/tmp-XXXXXX' });
282 console.log('Created temporary filename: ', tmpname);
287 One may want to cleanup the temporary files even when an uncaught exception
288 occurs. To enforce this, you can call the `setGracefulCleanup()` method:
291 var tmp = require('tmp');
293 tmp.setGracefulCleanup();
298 All options are optional :)
300 * `mode`: the file mode to create with, it fallbacks to `0600` on file creation and `0700` on directory creation
301 * `prefix`: the optional prefix, fallbacks to `tmp-` if not provided
302 * `postfix`: the optional postfix, fallbacks to `.tmp` on file creation
303 * `template`: [`mkstemp`][3] like filename template, no default
304 * `dir`: the optional temporary directory, fallbacks to system default (guesses from environment)
305 * `tries`: how many times should the function try to get a unique filename before giving up, default `3`
306 * `keep`: signals that the temporary file or directory should not be deleted on exit, default is `false`, means delete
307 * Please keep in mind that it is recommended in this case to call the provided `cleanupCallback` function manually.
308 * `unsafeCleanup`: recursively removes the created temporary directory, even when it's not empty. default is `false`
310 [1]: http://nodejs.org/
311 [2]: https://www.npmjs.com/browse/depended/tmp
312 [3]: http://www.kernel.org/doc/man-pages/online/pages/man3/mkstemp.3.html
313 [4]: https://raszi.github.io/node-tmp/
314 [5]: https://github.com/benjamingr/tmp-promise