uni-app vite-plugin-uni 报 ENOTEMPTY 错误:directory not empty

uni-app vite-plugin-uni 报 ENOTEMPTY 错误:directory not empty

开发环境 版本号 项目创建方式
Windows 10 HBuilderX

操作步骤:

function emptyDir(dir, skip) {  
    for (const file of fs_1.default.readdirSync(dir)) {  
        if (skip === null || skip === void 0 ? void 0 : skip.includes(file)) {  
            continue;  
        }  
        const abs = path_1.default.resolve(dir, file);  
        // baseline is Node 12 so can't use rmSync :(  
        if (fs_1.default.lstatSync(abs).isDirectory()) {  
            emptyDir(abs);  
            // fs_1.default.rmdirSync(abs); 原代码  
            fs_1.default.rmdirSync(abs, { recursive:true });  
        }  
        else {  
            fs_1.default.unlinkSync(abs);  
        }  
    }  
}

预期结果:

--

实际结果:

--

bug描述:

当目标文件夹中存在多个子文件夹,子文件夹中存在文件时报错

Error: ENOTEMPTY: directory not empty, rmdir '${path}'

文件位置
node_modules@dcloudio\uni-cli-shared\dist\fs.js


更多关于uni-app vite-plugin-uni 报 ENOTEMPTY 错误:directory not empty的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

同是这个报错,编译微信小程序的时候报的,vue2迁移vue3

更多关于uni-app vite-plugin-uni 报 ENOTEMPTY 错误:directory not empty的实战教程也可以访问 https://www.itying.com/category-93-b0.html


项目换到其他盘验证下

这个错误是由于 fs.rmdirSync 在递归删除目录时,如果目录非空会抛出 ENOTEMPTY 异常。虽然你添加了 { recursive: true } 参数,但在某些 Windows 环境下仍可能出现问题。

更可靠的解决方案是使用 fs.rmSync 替代 fs.rmdirSync,它提供了更稳定的递归删除功能:

if (fs_1.default.lstatSync(abs).isDirectory()) {
    emptyDir(abs);
    // 使用 rmSync 替代 rmdirSync
    fs_1.default.rmSync(abs, { recursive: true, force: true });
}

如果必须保持 Node 12 兼容性,可以修改递归逻辑:

function emptyDir(dir, skip) {
    for (const file of fs_1.default.readdirSync(dir)) {
        if (skip?.includes(file)) {
            continue;
        }
        const abs = path_1.default.resolve(dir, file);
        
        if (fs_1.default.lstatSync(abs).isDirectory()) {
            // 先递归清空子目录
            emptyDir(abs);
            // 再次检查目录是否为空
            const remaining = fs_1.default.readdirSync(abs);
            if (remaining.length === 0) {
                fs_1.default.rmdirSync(abs);
            } else {
                // 如果还有文件,可能是隐藏文件或权限问题
                console.warn(`Directory not empty: ${abs}, remaining: ${remaining}`);
            }
        } else {
            fs_1.default.unlinkSync(abs);
        }
    }
}

临时解决方案:清理 node_modules 后重新安装依赖:

rm -rf node_modules
npm install
回到顶部