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');
tempfs.open(function (err, file) {
if (err) { throw err; }
console.log(file.path, file.fd);
file.unlink(function () {
console.log('File delected');
});
file.unlink();
});
tempfs.mkdir({
dir: '.',
recursive: true,
track: true
}, function (err, dir) {
if (err) { throw err; }
console.log(dir.path, dir.recursive);
throw new Error('Since it is tracked, tempfs will remove it for you.');
dir.unlink();
});
APIs
options
-
limit: Number
The maximum number of chance to retry before throwing an error. It should
be a finite number. Default: 5
-
recursive: Boolean
Whether unlink()
should remove a directory recursively. Default: false
-
track: Boolean
If set to true
, let temp-fs manage the the current file/directory for you
even if the global tracking is off. If set to false
, don't let temp-fs
manage it even if the global tracking is on. 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.dir()
and
options.name
together and use the result as the customized
filename/pathname.
-
dir: String
Where to put the generated tempfile or tempdir. Also see options.name
above. Default: tempfs.dir()
-
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. Just like what
you do with the mktemp (3)
function in the C library.
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.
This switch does not affect manually tracked files through options.track
.
They will be removed automatically on exit.
Note: When an uncaught exception occurs, all tracked temporary files and
directories will be removed no matter it is on or off.
tempfs.dir()
Return the path of a system-provided tempdir as require('os').tmpdir()
does.
You should not make any assumption about whether the path contains a trailing
path separator, or it is a real path. On most system it is not a fixed path,
and it can be changed by the user environment. When in doubt, check it first.
tempfs.name([options])
Return a customized/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. If the file is not
tracked, it may throw when an error occurs or the first argument of the
callback function will be an Error object.
tempfs.openSync([options]): file
The synchronous version of tempfs.open
. It will throw when an error happens.
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. If the
directory is not tracked, it may throw when an error occurs or the first
argument of the callback function will be an Error object.
tempfs.mkdirSync([options]): dir
The synchronous version of tempfs.mkdir
. It will throw when an error happens.
tempfs.clear([callback])
Remove all tracked files and directories asynchronously.
tempfs.clearSync()
Remove all tracked files and directories synchronously.
License
The MIT License (MIT)