Reputation: 1390
fs.rmdir recursive stops working after app is packaged with Zeit/pkg
The following script works in 2 cases:
const os = require('os');
const fs = require('fs');
const path = require('path');
var tmpDir = path.join(os.tmpdir(), 'test');
if(!fs.existsSync(tmpDir)){
fs.mkdirSync(tmpDir);
fs.appendFileSync(path.join(tmpDir, 'message.txt'), 'data to append');
}
fs.rmdir(tmpDir, {recursive: true}, function(err){
if(err) throw err;
console.log('finished');
});
Otherwise it returns the following error:
internal/validators.js:117
throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
^
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received an instance of Buffer
at validateString (internal/validators.js:117:11)
at Object.dirname (path.js:583:5)
at isRootPath (pkg/prelude/bootstrap.js:168:26)
at fs.readdir (pkg/prelude/bootstrap.js:850:18)
at _rmchildren (internal/fs/rimraf.js:130:3)
at internal/fs/rimraf.js:117:16
at FSReqCallback.oncomplete (fs.js:154:23) {
code: 'ERR_INVALID_ARG_TYPE'
}
Tested on node12 and 13 on Windows 10.
Anyone know of a solution to this?
Thank you!
Upvotes: 0
Views: 470
Reputation: 1390
I ended up solving this by including and using the rimraf
package directly.
There's also the Del
package and for a simpler solution there's this bit of code
var fs = require('fs');
var deleteFolderRecursive = function(path) {
if( fs.existsSync(path) ) {
fs.readdirSync(path).forEach(function(file,index){
var curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
Upvotes: 0