node.js delete directory & file system
node.js delete directory & file system
delete a not empty directory
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的更多相关文章
- [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 ...
- 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. ...
- [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 ...
- Node.js NPM Tutorial
Node.js NPM Tutorial – How to Get Started with NPM? NPM is the core of any application that is devel ...
- Node.js Web 开发框架大全《静态文件服务器篇》
这篇文章与大家分享优秀的 Node.js 静态服务器模块.Node 是一个服务器端 JavaScript 解释器,它将改变服务器应该如何工作的概念.它的目标是帮助程序员构建高度可伸缩的应用程序,编写能 ...
- node.js监听文件变化
前言 随着前端技术的飞速发展,前端开发也从原始的刀耕火种,向着工程化效率化的方向发展.在各种开发框架之外,打包编译等技术也是层出不穷,开发体验也是越来越好.例如HMR,让我们的更新可以即时可见,告别了 ...
- 极简 Node.js 入门 - 3.1 File System API 风格
极简 Node.js 入门系列教程:https://www.yuque.com/sunluyong/node 本文更佳阅读体验:https://www.yuque.com/sunluyong/node ...
- 解决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 ...
- node.js & create file
node.js & create file node js create file if not exists https://nodejs.org/api/fs.html#fs_fs_ope ...
随机推荐
- Cannot assign requested address问题总结
Cannot assign requested address问题总结 - 简书 https://www.jianshu.com/p/51a953b789a4 python3 server.pyE07 ...
- PyPy CPython C++ connects programs written in C and C++ with a variety of high-level programming languages
PyPy 为什么会比 CPython 还要快? - 知乎 https://www.zhihu.com/question/19588346/answer/131977984 有个名词在现有的回答下面都没 ...
- ACCESS数据库偏移注入
偏移注入主要是针对知道表,但是不知道字段的ACCESS数据库. 比如我们已经知道了表名是 admin 判断字段数: http://192.168.74.136:8002/Production/PROD ...
- Spring5源码,@Autowired
一.@Autowired所具有的功能 二.在Spring中如何使用@Autowired 三.@Autowired注解背后的工作原理 一.@Autowired所具有的功能 @Autowired是一个用来 ...
- juniper srx系列配置端口映射 转载
http://www.cnblogs.com/pinpin/p/9895815.html
- 压缩文件 .zip.001 .zip.002合并
可以把名字特别长的命名为1 这样简单些 copy /B 1.zip.001+1.zip.002 1.zip
- 22.firewalld
1.firewalld 中常用的区域名称及策略规则 2.firewalld-cmd 命令中使用的参数以及作用 与 Linux 系统中其他的防火墙策略配置工具一样,使用firewalld 配置的防火墙策 ...
- 怎样将Sublime Text 设置成中文版(完整教程)
1.打开Sublime Text,使用快捷键Shift+Ctrl+P,弹出查找栏,如图: 2.在搜索框中输入关键字 install ,出现下拉选项,点击选择其中的:Package Control: I ...
- C#委托的进一步学习
一.委托的说明 namespace LearningCsharp { class Program { //定义一个委托,使用delegate加上方法签名 //将委托理解为存储方法的"数组&q ...
- Jenkins(2)docker容器中安装python3
前言 使用docker安装jenkins环境,jenkins构建的workspace目录默认是在容器里面构建的,如果我们想执行python3的代码,需进容器内部安装python3的环境. 进jenki ...