首页 > 解决方案 > Unable to delete directory using node.js

问题描述

I am trying to delete directory using fs module from node.js but as per my current code its throwing the following error.

Error:

Error:: { Error: ENOTEMPTY: directory not empty, rmdir '/app/uploads'
at Object.rmdirSync (fs.js:684:3)
at Object.usecaseWorkflow (/app/controller/usecaseWorkflowCtrl.js:97:32)
at process._tickCallback (internal/process/next_tick.js:68:7)
errno: -39,
syscall: 'rmdir',
code: 'ENOTEMPTY',
path: '/app/uploads' }

I am explaining my code below.

if (fs.existsSync(`${process.env['root_dir']}/uploads`)) {
    fs.rmdirSync(`${process.env['root_dir']}/uploads`, { 
          recursive: true, 
    });
}

Here I am trying to delete uploads directory present inside root directory but getting that error and also unable to delete. I am using node.js version 10. Can anybody help me to resolve this issue so that I can delete the directory successfully ?

标签: node.jsdirectoryfs

解决方案


只需使用 node.jsrimraf递归删除文件。

var rimraf = require("rimraf");
rimraf("/some/directory", function () { console.log("done"); });

推荐阅读