在使用Zeit/pkg打包app之后,fs.rmdir递归停止工作
以下脚本适用于2种情况:
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');
});否则,它将返回以下错误:
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'
}在node12上测试,在Windows 10上测试13。
有人知道解决这个问题的办法吗?
谢谢!
发布于 2020-04-11 02:24:55
最后,我通过直接包含和使用rimraf包来解决这个问题。
还有一个Del包,为了一个更简单的解决方案,这里有一些代码
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);
}
};https://stackoverflow.com/questions/61149538
复制相似问题