fs.rmdir recursive stops working after app is packaged with Zeit/pkg
The following script works in 2 cases:
- The script is ran unpackaged directly from node as node test.js and works perfectly
- The script is ran packaged and the tmpDir is empty. (most remove/comment fs.appendFileSync line)
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!
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