Remove a file or symbolic link without throwing an error when the target does not exist, as UNIX rm -f
does
const {unlink} = require('fs').promises;
const rmf = require('rmf');
// file.txt does not exist
(async () => {
await unlink('file.txt');
// will be rejected with Error: ENOENT: no such file or directory, unlink 'file.txt'
await rmf('file.txt');
//=> won't be rejected
})();
This library helps removing temporary files generated by test scripts, as they would not exist before the first execution.
npm install rmf
const rmf = require('rmf');
filePath: string
Buffer
Uint8Array
URL
Return: Promise<boolean>
It removes a file or symbolic link.
When it successfully remove the target, the Promise
will be resolved with true
. When the target doesn't exist, the Promise
will be resolved with false
.
When the operation fails with another reason, for example the target is a directory, the Promise
will be rejected.
(async () => {
// A file a.jpg exists, but b.jpg doesn't
await rmf('a.jpg'); //=> true
await rmf('b.jpg'); //=> false
await rmf(process.cwd());
// rejected with Error: EPERM: operation not permitted, unlink '/Users/shinnn/'
})();
ISC License © 2018 Shinnosuke Watanabe