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. GraphQL两年实战

    https://mp.weixin.qq.com/s/XIQ-0kRhjCe2ubBuhnhlQA

  2. python3编码转换

    str->bytes:encode编码 bytes->str:decode解码 字符串通过编码成为字节码,字节码通过解码成为字符串. >>> text = '我是文本' ...

  3. 【题解】CF952F 2 + 2 != 4

    题目传送门 首先这道题没有翻译,这是很奇怪的,经过了(bai)查(du)字(fan)典(yi)之后,你会发现,什么用都没有-- 楼下的 dalao 们给的解释非常的模糊(果然还是我太弱了),于是我自己 ...

  4. POJ2961_kmp

    Period Time Limit: 3000MS   Memory Limit: 30000KB   64bit IO Format: %I64d & %I64u Submit Status ...

  5. vue项目中如何引用tinymce

    最近公司在做一个CMS系统的项目,其中富文本编辑框用的很多,目前流行的也很多,包括wangEditor.TinyMCE.百度ueditor.kindeditor.CKEditor等.经过自己的一番翻箱 ...

  6. [一天一个进阶系列] - MyBatis基础篇

    前言:一直以来,很多人都是拿来主义,只停留在会使用的阶段,从未去研究挖掘其原理,剖析本质.现在慢慢探讨一下其内幕,抛砖引玉 一.简介 1)常用的持久化框架 Hibernate:是一款Java世界中最著 ...

  7. Maven多模块的2种依赖管理策略

    在Maven多模块的时候,管理依赖关系是非常重要的,各种依赖包冲突,查询问题起来非常复杂,于是就用到了<dependencyManagement>, 示例说明, 在父模块中: <de ...

  8. cassandra权威指南读书笔记--Cassandra架构(1)

    结构 集群-->数据中心-->机架-->节点. cassandra尽可能将数据副本存在多个数据中心,然后读取(查询路由到)尽可能在本地数据中心. 为了去中心化和分区容错性,使用gos ...

  9. DolphinScheduler源码分析之EntityTestUtils类

    1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license ...

  10. A. Little Pony and Expected Maximum

    Twilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she ...