Created
August 8, 2025 10:35
-
-
Save Wxh16144/edfd2c1220205ac2f1ce23d10f7825eb to your computer and use it in GitHub Desktop.
Node.js tmp Module Learning - Temporary File Auto-cleanup Demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| */ |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ref