Skip to content

Instantly share code, notes, and snippets.

@Wxh16144
Created August 8, 2025 10:35
Show Gist options
  • Select an option

  • Save Wxh16144/edfd2c1220205ac2f1ce23d10f7825eb to your computer and use it in GitHub Desktop.

Select an option

Save Wxh16144/edfd2c1220205ac2f1ce23d10f7825eb to your computer and use it in GitHub Desktop.
Node.js tmp Module Learning - Temporary File Auto-cleanup Demo
const tmp = require('tmp');
const fse = require('fs-extra');
const path = require('path');
const dir = tmp.dirSync({
prefix: path.basename(path.dirname(__filename))
});
console.log(`Temporary directory created: ${dir.name}`);
const file = path.join(dir.name, path.basename(__filename));
fse.writeFileSync(file, fse.readFileSync(__filename));
tmp.setGracefulCleanup();
setTimeout(() => {
console.log(`Clean`);
fse.emptyDirSync(dir.name);
setTimeout(() => {
console.log('End');
}, 1000);
}, 5000);
/**
To run this script multiple times concurrently, you can use the following shell command:
for i in {1..10}; do
(sleep 1 && node main.js) &
sleep 0.5
done
wait
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment