Gathering detailed insights and metrics for temp
Gathering detailed insights and metrics for temp
Gathering detailed insights and metrics for temp
Gathering detailed insights and metrics for temp
npm install temp
95.6
Supply Chain
98
Quality
76.7
Maintenance
100
Vulnerability
98.6
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
446 Stars
180 Commits
62 Forks
11 Watching
11 Branches
30 Contributors
Updated on 19 Jun 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-2.3%
1,509,104
Compared to previous day
Last week
3%
7,929,150
Compared to previous week
Last month
8.1%
33,226,229
Compared to previous month
Last year
25.3%
387,514,980
Compared to previous year
Temporary files, directories, and streams for Node.js.
Handles generating a unique file/directory name under the appropriate system temporary directory, changing the file to an appropriate mode, and supports automatic removal (if asked)
temp
has a similar API to the fs
module.
Supports v6.0.0+.
Please let me know if you have problems running it on a later version of Node.js or have platform-specific problems.
Install it using npm:
$ npm install temp
Or get it directly from: http://github.com/bruce/node-temp
You can create temporary files with open
and openSync
, temporary
directories with mkdir
and mkdirSync
, or you can get a unique name
in the system temporary directory with path
.
See the API section for more details on the API of temp
.
Working copies of the following examples can be found under the
examples
directory.
open
and openSync
)To create a temporary file use open
or openSync
, passing
them an optional prefix, suffix, or both (see below for details on
affixes). The object passed to the callback (or returned) has
path
and fd
keys:
1{ path: "/path/to/file", 2, fd: theFileDescriptor 3}
In this example we write to a temporary file and call out to grep
and
wc -l
to determine the number of time foo
occurs in the text. The
temporary file is chmod'd 0600
and cleaned up automatically when the
process at exit (because temp.track()
is called):
1var temp = require('temp'), 2 fs = require('fs'), 3 util = require('util'), 4 exec = require('child_process').exec; 5 6// Automatically track and cleanup files at exit 7temp.track(); 8 9// Fake data 10var myData = "foo\nbar\nfoo\nbaz"; 11 12// Process the data (note: error handling omitted) 13temp.open('myprefix', function(err, info) { 14 if (!err) { 15 fs.write(info.fd, myData, (err) => { 16 console.log(err); 17 }); 18 fs.close(info.fd, function(err) { 19 exec("grep foo '" + info.path + "' | wc -l", function(err, stdout) { 20 util.puts(stdout.trim()); 21 }); 22 }); 23 } 24});
track
)As noted in the example above, if you want temp to track the files and
directories it creates and handle removing those files and directories
on exit, you must call track()
. The track()
function is chainable,
and it's recommended that you call it when requiring the module.
1var temp = require("temp").track();
Why is this necessary? In pre-0.6 versions of temp, tracking was automatic. While this works great for scripts and Grunt tasks, it's not so great for long-running server processes. Since that's arguably what Node.js is for, you have to opt-in to tracking.
But it's easy.
cleanup
, cleanupSync
)When tracking, you can run cleanup()
and cleanupSync()
anytime
(cleanupSync()
will be run for you on process exit). An object will
be returned (or passed to the callback) with cleanup counts and
the file/directory tracking lists will be reset.
1> temp.cleanupSync(); 2{ files: 1, 3 dirs: 0 }
1> temp.cleanup(function(err, stats) { 2 console.log(stats); 3 }); 4{ files: 1, 5 dirs: 0 }
Note: If you're not tracking, an error ("not tracking") will be passed to the callback.
mkdir
, mkdirSync
)To create a temporary directory, use mkdir
or mkdirSync
, passing
it an optional prefix, suffix, or both (see below for details on affixes).
In this example we create a temporary directory, write to a file
within it, call out to an external program to create a PDF, and read
the result. While the external process creates a lot of additional
files, the temporary directory is removed automatically at exit (because
temp.track()
is called):
1var temp = require('temp'), 2 fs = require('fs'), 3 util = require('util'), 4 path = require('path'), 5 exec = require('child_process').exec; 6 7// Automatically track and cleanup files at exit 8temp.track(); 9 10// For use with ConTeXt, http://wiki.contextgarden.net 11var myData = "\\starttext\nHello World\n\\stoptext"; 12 13temp.mkdir('pdfcreator', function(err, dirPath) { 14 var inputPath = path.join(dirPath, 'input.tex') 15 fs.writeFile(inputPath, myData, function(err) { 16 if (err) throw err; 17 process.chdir(dirPath); 18 exec("texexec '" + inputPath + "'", function(err) { 19 if (err) throw err; 20 fs.readFile(path.join(dirPath, 'input.pdf'), function(err, data) { 21 if (err) throw err; 22 sys.print(data); 23 }); 24 }); 25 }); 26});
createWriteStream
)To create a temporary WriteStream, use 'createWriteStream', which sits
on top of fs.createWriteStream
. The return value is a
fs.WriteStream
with a path
property containing the temporary file
path for the stream. The path
is registered for removal when
temp.cleanup
is called (because temp.track()
is called).
1var temp = require('temp'); 2 3// Automatically track and cleanup files at exit 4temp.track(); 5 6var stream = temp.createWriteStream(); 7// stream.path contains the temporary file path for the stream 8stream.write("Some data"); 9// Maybe do some other things 10stream.end();
You can provide custom prefixes and suffixes when creating temporary
files and directories. If you provide a string, it is used as the prefix
for the temporary name. If you provide an object with prefix
,
suffix
and dir
keys, they are used for the temporary name.
Here are some examples:
"aprefix"
: A simple prefix, prepended to the filename; this is
shorthand for:{prefix: "aprefix"}
: A simple prefix, prepended to the filename{suffix: ".asuffix"}
: A suffix, appended to the filename
(especially useful when the file needs to be named with specific
extension for use with an external program).{prefix: "myprefix", suffix: "mysuffix"}
: Customize both affixes{dir: path.join(os.tmpdir(), "myapp")}
: default prefix and suffix
within a new temporary directory.null
: Use the defaults for files and directories (prefixes "f-"
and "d-"
, respectively, no suffixes).In this simple example we read a pdf
, write it to a temporary file with
a .pdf
extension, and close it.
1var fs = require('fs'), 2 temp = require('temp'); 3 4fs.readFile('/path/to/source.pdf', function(err, data) { 5 temp.open({suffix: '.pdf'}, function(err, info) { 6 if (err) throw err; 7 fs.write(info.fd, data, (err) => { 8 console.log(err) 9 }); 10 fs.close(info.fd, function(err) { 11 if (err) throw err; 12 // Do something with the file 13 }); 14 }); 15});
path
)If you just want a unique name in your temporary directory, use
path
:
1var fs = require('fs'); 2var tempName = temp.path({suffix: '.pdf'}); 3// Do something with tempName
Note: The file isn't created for you, and the mode is not changed -- and it
will not be removed automatically at exit. If you use path
, it's
all up to you.
1interface OpenFile { 2 path: string; 3 fd: number; 4} 5 6interface Stats { 7 files: number; 8 dirs: number; 9} 10 11interface AffixOptions { 12 prefix?: string; 13 suffix?: string; 14 dir?: string; 15} 16 17function track(value?: boolean): typeof temp; 18 19function mkdir(affixes: string | AffixOptions | undefined, callback: (err: any, dirPath: string) => void): void; 20function mkdir(affixes?: string | AffixOptions): Promise<string>; 21 22function mkdirSync(affixes?: string | AffixOptions): string; 23 24function open(affixes: string | AffixOptions | undefined, callback: (err: any, result: OpenFile) => void): void; 25function open(affixes?: string | AffixOptions): Promise<OpenFile>; 26 27function openSync(affixes?: string | AffixOptions): OpenFile; 28 29function path(affixes?: string | AffixOptions, defaultPrefix?: string): string; 30 31function cleanup(callback: (err: any, result: Stats) => void): void; 32function cleanup(): Promise<Stats>; 33 34function cleanupSync(): boolean | Stats; 35 36function createWriteStream(affixes?: string | AffixOptions): fs.WriteStream;
1$ npm test
You can find the repository at: http://github.com/bruce/node-temp
Issues/Feature Requests can be submitted at: http://github.com/bruce/node-temp/issues
I'd really like to hear your feedback, and I'd love to receive your pull-requests!
Copyright (c) 2010-2014 Bruce Williams. This software is licensed under the MIT License, see LICENSE for details.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 4/16 approved changesets -- score normalized to 2
Reason
8 existing vulnerabilities detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More