Gathering detailed insights and metrics for with-local-tmp-dir
Gathering detailed insights and metrics for with-local-tmp-dir
Gathering detailed insights and metrics for with-local-tmp-dir
Gathering detailed insights and metrics for with-local-tmp-dir
Creates a temporary folder inside cwd, cds inside the folder, runs a function, and removes the folder. Especially useful for testing.
npm install with-local-tmp-dir
Typescript
Module System
Min. Node Version
Node Version
NPM Version
TypeScript (89.62%)
Shell (10.38%)
Built with Next.js • Fully responsive • SEO optimized • Open source ready
Total Downloads
223,965
Last Day
39
Last Week
530
Last Month
3,366
Last Year
44,030
NOASSERTION License
6 Stars
253 Commits
1 Forks
2 Watchers
3 Branches
6 Contributors
Updated on Aug 25, 2025
Latest Version
6.0.0
Package Id
with-local-tmp-dir@6.0.0
Unpacked Size
14.67 kB
Size
5.53 kB
File Count
5
NPM Version
10.9.3
Node Version
20.19.4
Published on
Aug 07, 2025
Cumulative downloads
Total Downloads
Last Day
-18.8%
39
Compared to previous day
Last Week
-38.2%
530
Compared to previous week
Last Month
-52%
3,366
Compared to previous month
Last Year
56.9%
44,030
Compared to previous year
3
5
Creates a temporary folder inside cwd, cds inside the folder, runs a function, and removes the folder. Especially useful for testing.
This package can be used to write file-based tests with real files. See below for some examples.
1# npm 2$ npm install with-local-tmp-dir 3 4# Yarn 5$ yarn add with-local-tmp-dir
1import withLocalTmpDir from 'with-local-tmp-dir'; 2import fs from 'fs-extra'; 3 4await withLocalTmpDir(() => { 5 console.log(process.cwd()); 6 //> /Users/max/project/tmp-18815DudQxmdn03Rz 7 8 // Create some files 9 await fs.outputFile('foo.txt', 'foo bar'); 10 await fs.outputFile('bar.txt', 'foo bar'); 11}) 12// Now the folder does not exist anymore 13 14// The folder is removed even if an exception is thrown 15await withLocalTmpDir(() => { 16 throw new Error('File could not be found') 17});
Instead of a function you can reset the state yourself instead of passing a function, i.e. change to the previous folder and remove the folder:
1import withLocalTmpDir from 'with-local-tmp-dir'; 2import fs from 'fs-extra'; 3 4// You can still pass options 5const reset = await withLocalTmpDir(); 6 7console.log(process.cwd()); 8//> /Users/max/project/tmp-18815DudQxmdn03Rz 9 10// Create some files 11await fs.outputFile('foo.txt', 'foo bar'); 12await fs.outputFile('bar.txt', 'foo bar'); 13 14await reset(); 15 16// Now the folder does not exist anymore
You can pass an options object to the function:
mode
: The file mode to create with, it fallbacks to 0700
prefix
: The optional prefix, fallbacks to tmp-
if not providedpostfix
: The optional postfixtemplate
: mkstemp
like filename template, no defaultdir
: The base directory, falls back to cwdtries
: How many times should the function try to get a unique filename before giving up, default 3
keep
: Signals that the temporary file or directory should not be deleted on exit, default is false
, means deleteunsafeCleanup
: Recursively removes the created temporary directory, even when it's not empty. default is true
1import withLocalTmpDir from 'with-local-tmp-dir'; 2import fs from 'fs-extra'; 3 4// do not cleanup if there are files 5await withLocalTmpDir(() => { 6 await fs.outputFile('foo.txt', 'foo bar'); 7}, { unsafeCleanup: false }); 8 9// run in a subfolder 10await withLocalTmpDir(() => { 11 console.log(process.cwd()) 12 //> /Users/max/project/foo/tmp-18815DudQxmdn03Rz 13}, { dir: 'foo' }) 14 15// use a different prefix 16await withLocalTmpDir(() => { 17 console.log(process.cwd()) 18 //> /Users/max/project/foo-18815DudQxmdn03Rz 19}, { prefix: 'foo' })
The most common use case for this package is probably unit tests with Mocha or Jest. The package itself is framework-agnostic, so you can use any of them. To make life easier when writing multiple files with multi-line text content, there are two handy packages: output-files and endent.
The following is an example for Mocha:
1const withLocalTmpDir = require('with-local-tmp-dir') 2const outputFiles = require('output-files') 3const endent = require('endent') 4 5const funcToTest = require('.') 6 7beforeEach(async function () { 8 this.resetWithLocalTmpDir = await withLocalTmpDir() 9}) 10 11afterEach(async function () { 12 await this.resetWithLocalTmpDir() 13}) 14 15it('works', async () => { 16 await outputFiles({ 17 'foo.txt': endent` 18 Lorem ipsum 19 dolor sit 20 `, 21 'index.js': endent` 22 export default { 23 foo: 1, 24 bar: 2, 25 } 26 `, 27 }) 28 funcToTest() 29})
It is also possible to test libraries that make use of Git. You can instantiate a local Git repository inside the temporary folder and run Git operations on it:
1const withLocalTmpDir = require('with-local-tmp-dir') 2const fs = require('fs-extra') 3const execa = require('execa') 4 5beforeEach(async function () { 6 this.resetWithLocalTmpDir = await withLocalTmpDir() 7}) 8 9afterEach(async function () { 10 await this.resetWithLocalTmpDir() 11}) 12 13it('works', async () => { 14 await execa.command('git init') 15 await execa.command('git config user.email "foo@bar.de"') 16 await execa.command('git config user.name "foo"') 17 18 await outputFile('foo.txt', 'foo bar') 19 20 await execa.command('git add .') 21 await execa.command('git commit -m "feat: init"') 22 funcToTest() 23})
Be careful though, if the repository is not properly initialized, your user Git config might be overridden!
Are you missing something or want to contribute? Feel free to file an issue or a pull request! ⚙️
Hey, I am Sebastian Landwehr, a freelance web developer, and I love developing web apps and open source packages. If you want to support me so that I can keep packages up to date and build more helpful tools, you can donate here:
If you want to send me a one time donation. The coffee is pretty good 😊.
Also for one time donations if you like PayPal.
Here you can support me regularly, which is great so I can steadily work on projects.
Thanks a lot for your support! ❤️
No vulnerabilities found.