{"id":13526918,"url":"https://github.com/bruce/node-temp","last_synced_at":"2025-04-01T08:30:34.926Z","repository":{"id":928294,"uuid":"696592","full_name":"bruce/node-temp","owner":"bruce","description":"Temporary File, Directory, and Stream support for Node.js","archived":false,"fork":false,"pushed_at":"2022-12-30T18:58:25.000Z","size":219,"stargazers_count":449,"open_issues_count":6,"forks_count":61,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-03-26T14:03:26.853Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://github.com/bruce/node-temp","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bruce.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2010-06-01T03:52:34.000Z","updated_at":"2025-01-09T12:31:55.000Z","dependencies_parsed_at":"2023-01-13T10:47:23.643Z","dependency_job_id":null,"html_url":"https://github.com/bruce/node-temp","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bruce%2Fnode-temp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bruce%2Fnode-temp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bruce%2Fnode-temp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bruce%2Fnode-temp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bruce","download_url":"https://codeload.github.com/bruce/node-temp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246117586,"owners_count":20726066,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T06:01:37.449Z","updated_at":"2025-04-01T08:30:34.515Z","avatar_url":"https://github.com/bruce.png","language":"JavaScript","readme":"![Image of node-temp logo](https://raw.githubusercontent.com/bruce/node-temp/master/media/A5.jpg)\n=========\n\nTemporary files, directories, and streams for Node.js.\n\nHandles generating a unique file/directory name under the appropriate\nsystem temporary directory, changing the file to an appropriate mode,\nand supports automatic removal (if asked)\n\n`temp` has a similar API to the `fs` module.\n\nNode.js Compatibility\n---------------------\n\nSupports v6.0.0+.\n\n[![Build Status](https://travis-ci.org/bruce/node-temp.png)](https://travis-ci.org/bruce/node-temp)\n\nPlease let me know if you have problems running it on a later version of Node.js or\nhave platform-specific problems.\n\nInstallation\n------------\n\nInstall it using [npm](http://github.com/isaacs/npm):\n\n    $ npm install temp\n\nOr get it directly from:\nhttp://github.com/bruce/node-temp\n\nSynopsis\n--------\n\nYou can create temporary files with `open` and `openSync`, temporary\ndirectories with `mkdir` and `mkdirSync`, or you can get a unique name\nin the system temporary directory with `path`.\n\nSee the [API section](https://github.com/bruce/node-temp#API) for more details on the API of `temp`.\n\nWorking copies of the following examples can be found under the\n`examples` directory.\n\n### Temporary Files (`open` and `openSync`)\n\nTo create a temporary file use `open` or `openSync`, passing\nthem an optional prefix, suffix, or both (see below for details on\naffixes). The object passed to the callback (or returned) has\n`path` and `fd` keys:\n\n```javascript\n{ path: \"/path/to/file\",\n, fd: theFileDescriptor\n}\n```\n\nIn this example we write to a temporary file and call out to `grep` and\n`wc -l` to determine the number of time `foo` occurs in the text.  The\ntemporary file is chmod'd `0600` and cleaned up automatically when the\nprocess at exit (because `temp.track()` is called):\n\n```javascript\nvar temp = require('temp'),\n    fs   = require('fs'),\n    util  = require('util'),\n    exec = require('child_process').exec;\n\n// Automatically track and cleanup files at exit\ntemp.track();\n\n// Fake data\nvar myData = \"foo\\nbar\\nfoo\\nbaz\";\n\n// Process the data (note: error handling omitted)\ntemp.open('myprefix', function(err, info) {\n  if (!err) {\n    fs.write(info.fd, myData, (err) =\u003e {\n\t\tconsole.log(err);\n\t});\n    fs.close(info.fd, function(err) {\n      exec(\"grep foo '\" + info.path + \"' | wc -l\", function(err, stdout) {\n        util.puts(stdout.trim());\n      });\n    });\n  }\n});\n```\n\n### Want Cleanup? Make sure you ask for it. (`track`)\n\nAs noted in the example above, if you want temp to track the files and\ndirectories it creates and handle removing those files and directories\non exit, you must call `track()`. The `track()` function is chainable,\nand it's recommended that you call it when requiring the module.\n\n```javascript\nvar temp = require(\"temp\").track();\n```\n\nWhy is this necessary? In pre-0.6 versions of temp, tracking was\nautomatic. While this works great for scripts and\n[Grunt tasks](http://gruntjs.com/), it's not so great for long-running\nserver processes. Since that's arguably what Node.js is _for_, you\nhave to opt-in to tracking.\n\nBut it's easy.\n\n#### Cleanup anytime (`cleanup`, `cleanupSync`)\n\nWhen tracking, you can run `cleanup()` and `cleanupSync()` anytime\n(`cleanupSync()` will be run for you on process exit). An object will\nbe returned (or passed to the callback) with cleanup counts and\nthe file/directory tracking lists will be reset.\n\n```javascript\n\u003e temp.cleanupSync();\n{ files: 1,\n  dirs:  0 }\n```\n\n```javascript\n\u003e temp.cleanup(function(err, stats) {\n    console.log(stats);\n  });\n{ files: 1,\n  dirs:  0 }\n```\n\nNote: If you're not tracking, an error (\"not tracking\") will be passed\nto the callback.\n\n### Temporary Directories (`mkdir`, `mkdirSync`)\n\nTo create a temporary directory, use `mkdir` or `mkdirSync`, passing\nit an optional prefix, suffix, or both (see below for details on affixes).\n\nIn this example we create a temporary directory, write to a file\nwithin it, call out to an external program to create a PDF, and read\nthe result.  While the external process creates a lot of additional\nfiles, the temporary directory is removed automatically at exit (because\n`temp.track()` is called):\n\n```javascript\nvar temp = require('temp'),\n    fs   = require('fs'),\n    util = require('util'),\n    path = require('path'),\n    exec = require('child_process').exec;\n\n// Automatically track and cleanup files at exit\ntemp.track();\n\n// For use with ConTeXt, http://wiki.contextgarden.net\nvar myData = \"\\\\starttext\\nHello World\\n\\\\stoptext\";\n\ntemp.mkdir('pdfcreator', function(err, dirPath) {\n  var inputPath = path.join(dirPath, 'input.tex')\n  fs.writeFile(inputPath, myData, function(err) {\n    if (err) throw err;\n    process.chdir(dirPath);\n    exec(\"texexec '\" + inputPath + \"'\", function(err) {\n      if (err) throw err;\n      fs.readFile(path.join(dirPath, 'input.pdf'), function(err, data) {\n        if (err) throw err;\n        sys.print(data);\n      });\n    });\n  });\n});\n```\n\n### Temporary Streams (`createWriteStream`)\n\nTo create a temporary WriteStream, use 'createWriteStream', which sits\non top of `fs.createWriteStream`. The return value is a\n`fs.WriteStream` with a `path` property containing the temporary file\npath for the stream. The `path` is registered for removal when\n`temp.cleanup` is called (because `temp.track()` is called).\n\n```javascript\nvar temp = require('temp');\n\n// Automatically track and cleanup files at exit\ntemp.track();\n\nvar stream = temp.createWriteStream();\n// stream.path contains the temporary file path for the stream\nstream.write(\"Some data\");\n// Maybe do some other things\nstream.end();\n```\n\n### Affixes (options)\n\nYou can provide custom prefixes and suffixes when creating temporary\nfiles and directories. If you provide a string, it is used as the prefix\nfor the temporary name. If you provide an object with `prefix`,\n`suffix` and `dir` keys, they are used for the temporary name.\n\nHere are some examples:\n\n* `\"aprefix\"`: A simple prefix, prepended to the filename; this is\n  shorthand for:\n* `{prefix: \"aprefix\"}`: A simple prefix, prepended to the filename\n* `{suffix: \".asuffix\"}`: A suffix, appended to the filename\n  (especially useful when the file needs to be named with specific\n  extension for use with an external program).\n* `{prefix: \"myprefix\", suffix: \"mysuffix\"}`: Customize both affixes\n* `{dir: path.join(os.tmpdir(), \"myapp\")}`: default prefix and suffix\n  within a new temporary directory.\n* `null`: Use the defaults for files and directories (prefixes `\"f-\"`\n  and `\"d-\"`, respectively, no suffixes).\n\nIn this simple example we read a `pdf`, write it to a temporary file with\na `.pdf` extension, and close it.\n\n```javascript\nvar fs   = require('fs'),\n    temp = require('temp');\n\nfs.readFile('/path/to/source.pdf', function(err, data) {\n  temp.open({suffix: '.pdf'}, function(err, info) {\n    if (err) throw err;\n    fs.write(info.fd, data, (err) =\u003e {\n\t\t\tconsole.log(err)\n\t\t});\n    fs.close(info.fd, function(err) {\n      if (err) throw err;\n      // Do something with the file\n    });\n  });\n});\n```\n\n### Just a path, please (`path`)\n\nIf you just want a unique name in your temporary directory, use\n`path`:\n\n```javascript\nvar fs = require('fs');\nvar tempName = temp.path({suffix: '.pdf'});\n// Do something with tempName\n```\n\nNote: The file isn't created for you, and the mode is not changed  -- and it\nwill not be removed automatically at exit.  If you use `path`, it's\nall up to you.\n\n\nAPI\n-------\n\n```ts\ninterface OpenFile {\n\tpath: string;\n\tfd: number;\n}\n\ninterface Stats {\n\tfiles: number;\n\tdirs: number;\n}\n\ninterface AffixOptions {\n\tprefix?: string;\n\tsuffix?: string;\n\tdir?: string;\n}\n\nfunction track(value?: boolean): typeof temp;\n\nfunction mkdir(affixes: string | AffixOptions | undefined, callback: (err: any, dirPath: string) =\u003e void): void;\nfunction mkdir(affixes?: string | AffixOptions): Promise\u003cstring\u003e;\n\nfunction mkdirSync(affixes?: string | AffixOptions): string;\n\nfunction open(affixes: string | AffixOptions | undefined, callback: (err: any, result: OpenFile) =\u003e void): void;\nfunction open(affixes?: string | AffixOptions): Promise\u003cOpenFile\u003e;\n\nfunction openSync(affixes?: string | AffixOptions): OpenFile;\n\nfunction path(affixes?: string | AffixOptions, defaultPrefix?: string): string;\n\nfunction cleanup(callback: (err: any, result: Stats) =\u003e void): void;\nfunction cleanup(): Promise\u003cStats\u003e;\n\nfunction cleanupSync(): boolean | Stats;\n\nfunction createWriteStream(affixes?: string | AffixOptions): fs.WriteStream;\n```\n\n\nTesting\n-------\n\n```sh\n$ npm test\n```\n\nContributing\n------------\n\nYou can find the repository at:\nhttp://github.com/bruce/node-temp\n\nIssues/Feature Requests can be submitted at:\nhttp://github.com/bruce/node-temp/issues\n\nI'd really like to hear your feedback, and I'd love to receive your\npull-requests!\n\nCopyright\n---------\n\nCopyright (c) 2010-2014 Bruce Williams. This software is licensed\nunder the MIT License, see LICENSE for details.\n","funding_links":[],"categories":["Repository"],"sub_categories":["Filesystem"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbruce%2Fnode-temp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbruce%2Fnode-temp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbruce%2Fnode-temp/lists"}