What is temp-fs?
The 'temp-fs' npm package provides a simple and efficient way to create and manage temporary files and directories. It is particularly useful for applications that need to handle temporary data storage, such as during testing or when processing files that do not need to be permanently stored.
What are temp-fs's main functionalities?
Create Temporary File
This feature allows you to create a temporary file. The file will be automatically deleted when the process exits.
const temp = require('temp-fs');
const tempFile = temp.createFile();
console.log('Temporary file created at:', tempFile.path);
Create Temporary Directory
This feature allows you to create a temporary directory. The directory will be automatically deleted when the process exits.
const temp = require('temp-fs');
const tempDir = temp.createDir();
console.log('Temporary directory created at:', tempDir.path);
Custom Cleanup
This feature allows you to create temporary files or directories with custom cleanup options. You can choose to keep the file or directory and manually clean it up later.
const temp = require('temp-fs');
const tempFile = temp.createFile({ keep: true });
console.log('Temporary file created at:', tempFile.path);
// Manually cleanup
tempFile.cleanupSync();
Other packages similar to temp-fs
tmp
'tmp' is another npm package for creating temporary files and directories. It offers similar functionality to 'temp-fs' but with a slightly different API. 'tmp' also provides automatic cleanup of temporary files and directories.
temp
'temp' is a package that provides temporary file and directory creation with automatic cleanup. It is similar to 'temp-fs' but has a more minimalistic API and fewer configuration options.
fs-extra
'fs-extra' extends the native Node.js 'fs' module with additional methods, including methods for creating temporary files and directories. While 'fs-extra' is more general-purpose, it can be used to achieve similar functionality to 'temp-fs'.
temp-fs
A temporary file and directory creator for io.js and Node.js™.
Just like raszi/node-tmp and bruce/node-temp, it can safely create temporary
files and directories without worrying a lot of about race conditions as long
as you don't do some tricky things. ;-) You can also let this module track the
files or directories you created and delete them when the program exits.
Installation
npm install temp-fs
var tempfs = require('temp-fs');
ts.open(function (err, file) {
if (err) {
throw err;
}
console.log(file.path, file.fd);
file.unlink(function () {
console.log('File delected');
});
file.unlink();
});
APIs
options
-
limit: Number
The maximum number of chance to retry before throwing an error. Default: 5
-
recursive: Boolean
Whether unlink()
should remove a directory recursively. Default: false
-
track: Boolean
If set to true
, let tempfs manage the the current file/directory for
you. If set to false
, don't let tempfs manage it. Otherwise, use the
current global setting.
-
mode: Number
File mode (default: 0600) or directory mode (default: 0700) to use.
-
name: String
If set, join the two paths options.dir || tempfs.tmpdir()
and
options.name
together and use the result as the custom
filename/pathname.
-
dir: String
See options.name
above. Default: tempfs.tmpdir()
-
prefix: String
The prefix for the generated random name. Default: "tmp-"
-
suffix: String
The suffix for the generated random name. Default: ""
-
template: String
A string containing some capital letters Xs for substitution with random
characters. Then it is used as part of the filename/dirname.
tempfs.track(on = true)
Use it to switch global files/directories tracking on or off. Turn it on if
you don't want to manually delete everything. When it is turned off, all
recorded files and directories will not be removed but still kept in case it
is turned on again before the program exits.
tempfs.dir()
Return the path of a system-provided tempdir.
tempfs.name([options])
Return a custom/random filename/dirname. Options are documented at
options.
tempfs.open([options], [callback])
Try to open a unique tempfile asynchronously. The callback function receives
two arguments error
and file
. If error
is null, file
has these
properties:
path
: The absolute path to the tempfile.fd
: An integer file descriptor.unlink
: A special function for you to delete the file. If you invoke it
with a callback function, it will become asynchronous.
tempfs.openSync([options]): file
The synchronous version of tempfs.open
.
tempfs.mkdir([options], [callback])
Try to create a new tempdir asynchronously. The callback function receives two
arguments error
and dir
. If error
is null, dir
has these properties:
path
: The absolute path to the tempdir.recursive
: Whether unlink() will remove the tempdir recursively.unlink
: A special function for you to remove the directory. If you
invoke it with a callback function, it will become asynchronous.
tempfs.mkdirSync([options]): dir
The synchronous version of tempfs.mkdir
.
tempfs.clear([callback])
Remove all tracked files and directories asynchronously.
tempfs.clearSync()
Remove all tracked files and directories synchronously.
License
The MIT License (MIT)
Copyright (c) 2015 Jak Wings
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.