node.js delete directory & file system

delete a not empty directory

https://nodejs.org/api/fs.htm

fs.rmdir(path[, options], callback)
fs.rmdirSync(path[, options])

recursive: true

In a Node.js application, you can use the fs.rmdir() method to delete a directory.

This method works asynchronously to remove the directory.

If the directory is not empty, you can pass an optional recursive flag to delete all nested files and folders recursively.

const fs = require('fs');

// directory path
const dir = 'temp'; // delete directory recursively
fs.rmdir(dir, { recursive: true }, (err) => {
if (err) {
throw err;
} console.log(`${dir} is deleted!`);
});
const fs = require('fs');

// directory path
const dir = 'temp'; // delete directory recursively
try {
fs.rmdirSync(dir, { recursive: true }); console.log(`${dir} is deleted!`);
} catch (err) {
console.error(`Error while deleting ${dir}.`);
}

del

https://www.npmjs.com/package/del

$ npm i -D del

https://github.com/sindresorhus/del/blob/master/index.js

trash

https://github.com/sindresorhus/trash/blob/master/index.js

rimraf

https://www.npmjs.com/package/rimraf

$ npm i -D rimraf

https://github.com/isaacs/rimraf/blob/master/rimraf.js

refs

https://attacomsian.com/blog/nodejs-delete-directory

https://geedew.com/remove-a-directory-that-is-not-empty-in-nodejs/


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);
}
};

unlink

https://stackoverflow.com/questions/39963966/can-fs-unlink-delete-a-empty-or-non-empty-folder

https://stackoverflow.com/questions/18052762/remove-directory-which-is-not-empty/32197381

refs



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


node.js delete directory & file system的更多相关文章

  1. [Node.js] Read a File in Node.js with fs.readFile and fs.readFileSync

    We'll read a csv file in node.js both synchronously, and asynchronously. The file we're reading is a ...

  2. Getting Started with Mongoose and Node.js – A Sample Comments System | Dev Notes

    In this post, we’re going to be creating a sample comments system using Node, Express and Mongoose.  ...

  3. [Node.js] Trigger a File Download in Express

    Downloading and saving a file is a common scenario when building out your web application. Using Exp ...

  4. Node.js NPM Tutorial

    Node.js NPM Tutorial – How to Get Started with NPM? NPM is the core of any application that is devel ...

  5. Node.js Web 开发框架大全《静态文件服务器篇》

    这篇文章与大家分享优秀的 Node.js 静态服务器模块.Node 是一个服务器端 JavaScript 解释器,它将改变服务器应该如何工作的概念.它的目标是帮助程序员构建高度可伸缩的应用程序,编写能 ...

  6. node.js监听文件变化

    前言 随着前端技术的飞速发展,前端开发也从原始的刀耕火种,向着工程化效率化的方向发展.在各种开发框架之外,打包编译等技术也是层出不穷,开发体验也是越来越好.例如HMR,让我们的更新可以即时可见,告别了 ...

  7. 极简 Node.js 入门 - 3.1 File System API 风格

    极简 Node.js 入门系列教程:https://www.yuque.com/sunluyong/node 本文更佳阅读体验:https://www.yuque.com/sunluyong/node ...

  8. 解决Warning: unlink(/storage/cache/cache.catalog.language.1556158719): No such file or directory in /system/library/cache/file.php on line 68问题

    ytkah在调试opencart项目时提示Warning: unlink(/storage/cache/cache.catalog.language.1556158719): No such file ...

  9. node.js & create file

    node.js & create file node js create file if not exists https://nodejs.org/api/fs.html#fs_fs_ope ...

随机推荐

  1. Java面向对象(一)----初次见面

    面向对象 面向过程:根据业务逻辑从上到下写代码 函数式编程:对一些功能的代码封装到函数中,日后无需重复编写,直接调用函数就可以了 面向对象:将所有的功能进行封装,面对的事封装了功能的实体(对象),即面 ...

  2. 为什么要内存对齐?Go 语言有时也需要考虑对齐的问题

    https://mp.weixin.qq.com/s/NE6Y2TVxrl-cpY-36puQcQ

  3. 向同一个模型的外键反向关联名称产生了冲突 Django迁移

    向同一个模型的外键反向关联名称产生了冲突 一个模型中有两个外键指向同一张表时,创建迁移模型时报错:" HINT: Add or change a related_name argument ...

  4. Map转换为格式化的YAML字符串

    yaml与java对象的互转 yaml与java对象的互转有snakeyaml <dependency> <groupId>org.yaml</groupId> & ...

  5. 小麦苗数据库巡检脚本,支持Oracle、MySQL、SQL Server和PG等数据库

    目录 一.巡检脚本简介 二.巡检脚本特点 三.巡检结果展示 1.Oracle数据库 2.MySQL数据库 3.SQL Server数据库 4.PG数据库 5.OS信息 四.脚本运行方式 1.Oracl ...

  6. 十五:SpringBoot-配置Actuator组件,实现系统监控

    SpringBoot-配置Actuator组件,实现系统监控 1.Actuator简介 1.1 监控组件作用 1.2 监控分类 2.SpringBoot整合Actuator 2.1 核心依赖Jar包 ...

  7. Spring Boot RestTemplate文件上传

    @ResponseBody @RequestMapping(value = "/upload.do", method = RequestMethod.POST) public St ...

  8. js创建javaMap

    /** * Simple Map * var m = new Map(); * m.put('key','value'); * var v_otherMap = v_m.toMapString();* ...

  9. DEDECMS:解决BMP、jpeg图片或MP4视频无法上传和在后台无法显示

    一.BMP图片无法上传解决方法: 1.修改配置文件: 在include-->dialog的文件夹下, select_images_post.php--> 把 $sparr = Array( ...

  10. Eclipse中配置Junit

    在要使用Junit的project名上,点击properties-->Java Build Path-->Libraries,点击Add Library 选择Junit 选择Junit 4 ...